<!DOCTYPE html><html lang="en" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【深度学习】实例第四部分：PaddlePaddle | Yang's Harbor</title><meta name="keywords" content="学习,记录,Python,笔记,深度学习"><meta name="author" content="✨YangSier✨,hobart.yang@qq.com"><meta name="copyright" content="✨YangSier✨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="description" content="注意：全部代码为PaddlePaddle1版本的代码 Helloworld1234567891011121314# helloworld示例import paddle.fluid as fluid# 创建两个类型为int64, 形状为1*1张量x &#x3D; fluid.layers.fill_constant(shape&#x3D;[1], dtype&#x3D;&quot;int64&quot;, value&#x3D;5)y &#x3D;">
<meta property="og:type" content="article">
<meta property="og:title" content="【深度学习】实例第四部分：PaddlePaddle">
<meta property="og:url" content="https://discover304.top/2021/12/01/2021q4/108-3-dl-ex/index.html">
<meta property="og:site_name" content="Yang&#39;s Harbor">
<meta property="og:description" content="注意：全部代码为PaddlePaddle1版本的代码 Helloworld1234567891011121314# helloworld示例import paddle.fluid as fluid# 创建两个类型为int64, 形状为1*1张量x &#x3D; fluid.layers.fill_constant(shape&#x3D;[1], dtype&#x3D;&quot;int64&quot;, value&#x3D;5)y &#x3D;">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://image.discover304.top/ai/dl/space_work.jpg?imageView2/2/h/300">
<meta property="article:published_time" content="2021-12-01T02:16:47.000Z">
<meta property="article:modified_time" content="2021-12-25T06:48:18.000Z">
<meta property="article:author" content="✨YangSier✨">
<meta property="article:tag" content="学习">
<meta property="article:tag" content="记录">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="笔记">
<meta property="article:tag" content="深度学习">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://image.discover304.top/ai/dl/space_work.jpg?imageView2/2/h/300"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://discover304.top/2021/12/01/2021q4/108-3-dl-ex/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="preconnect" href="//zz.bdstatic.com"/><meta name="google-site-verification" content="ilqpfk3vkgzDNNikz_V37-DOvRyi5wv4Hoi_eyBqvTg"/><meta name="msvalidate.01" content="49D9A50CCF9744E17274791468EDB517"/><meta name="baidu-site-verification" content="code-V24KosyVh1"/><meta name="360-site-verification" content="bd8859c3d74dfa3e8aeee9db30c94bd2"/><meta name="yandex-verification" content="f28ec9bbd50c56f5"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?8030f6052f2fed6a4704d96619f090d6";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><link rel="stylesheet" href="/css/font.css" media="print" onload="this.media='all'"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"We didn't find any results for the search: ${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: {"limitDay":365,"position":"top","messagePrev":"It has been","messageNext":"days since the last update, the content of the article may be outdated."},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: 'days',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: {"limitCount":200,"languages":{"author":"Author: ✨YangSier✨","link":"Link: ","source":"Source: Yang's Harbor","info":"Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source."}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"Traditional Chinese Activated Manually","cht_to_chs":"Simplified Chinese Activated Manually","day_to_night":"Dark Mode Activated Manually","night_to_day":"Light Mode Activated Manually","bgLight":"#ffc910","bgDark":"#02c3f6","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isanchor: true
};

var saveToLocal = {
  set: function setWithExpiry(key, value, ttl) {
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}

// https://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript
const getScript = url => new Promise((resolve, reject) => {
  const script = document.createElement('script')
  script.src = url
  script.async = true
  script.onerror = reject
  script.onload = script.onreadystatechange = function() {
    const loadState = this.readyState
    if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
    script.onload = script.onreadystatechange = null
    resolve()
  }
  document.head.appendChild(script)
})</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-12-25 14:48:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = 'false'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}})()</script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="Yang's Harbor" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">Loading...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" data-lazy-src="/img/head.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">243</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">88</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">Categories</div><div class="length-num">23</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Links</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> Articles</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archive</span></a></li><li><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Category</span></a></li><li><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></li></ul></div></div></div></div><div id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://image.discover304.top/ai/dl/space_work.jpg?imageView2/2/h/300)"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Yang's Harbor</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> Search</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Links</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> Articles</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archive</span></a></li><li><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Category</span></a></li><li><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【深度学习】实例第四部分：PaddlePaddle</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2021-12-01T02:16:47.000Z" title="Created 2021-12-01 10:16:47">2021-12-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2021-12-25T06:48:18.000Z" title="Updated 2021-12-25 14:48:18">2021-12-25</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/NoteBook/">NoteBook</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/NoteBook/PythonNote/">PythonNote</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">Word count:</span><span class="word-count">8.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">Reading time:</span><span>39min</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>注意：全部代码为PaddlePaddle1版本的代码</p>
<h2 id="Helloworld"><a href="#Helloworld" class="headerlink" title="Helloworld"></a>Helloworld</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># helloworld示例</span></span><br><span class="line"><span class="keyword">import</span> paddle.fluid <span class="keyword">as</span> fluid</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建两个类型为int64, 形状为1*1张量</span></span><br><span class="line">x = fluid.layers.fill_constant(shape=[<span class="number">1</span>], dtype=<span class="string">&quot;int64&quot;</span>, value=<span class="number">5</span>)</span><br><span class="line">y = fluid.layers.fill_constant(shape=[<span class="number">1</span>], dtype=<span class="string">&quot;int64&quot;</span>, value=<span class="number">1</span>)</span><br><span class="line">z = x + y <span class="comment"># z只是一个对象,没有run,所以没有值</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建执行器</span></span><br><span class="line">place = fluid.CPUPlace() <span class="comment"># 指定在CPU上执行</span></span><br><span class="line">exe = fluid.Executor(place) <span class="comment"># 创建执行器</span></span><br><span class="line">result = exe.run(fluid.default_main_program(),</span><br><span class="line">                 fetch_list=[z]) <span class="comment">#返回哪个结果</span></span><br><span class="line"><span class="built_in">print</span>(result) <span class="comment"># result为多维张量</span></span><br></pre></td></tr></table></figure>

<h2 id="张量操作"><a href="#张量操作" class="headerlink" title="张量操作"></a>张量操作</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> paddle.fluid <span class="keyword">as</span> fluid</span><br><span class="line"><span class="keyword">import</span> numpy</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建x, y两个2行3列，类型为float32的变量(张量)</span></span><br><span class="line">x = fluid.layers.data(name=<span class="string">&quot;x&quot;</span>, shape=[<span class="number">2</span>, <span class="number">3</span>], dtype=<span class="string">&quot;float32&quot;</span>)</span><br><span class="line">y = fluid.layers.data(name=<span class="string">&quot;y&quot;</span>, shape=[<span class="number">2</span>, <span class="number">3</span>], dtype=<span class="string">&quot;float32&quot;</span>)</span><br><span class="line"></span><br><span class="line">x_add_y = fluid.layers.elementwise_add(x, y)  <span class="comment"># 两个张量按元素相加</span></span><br><span class="line">x_mul_y = fluid.layers.elementwise_mul(x, y)  <span class="comment"># 两个张量按元素相乘</span></span><br><span class="line"></span><br><span class="line">place = fluid.CPUPlace()  <span class="comment"># 指定在CPU上执行</span></span><br><span class="line">exe = fluid.Executor(place)  <span class="comment"># 创建执行器</span></span><br><span class="line">exe.run(fluid.default_startup_program())  <span class="comment"># 初始化网络</span></span><br><span class="line"></span><br><span class="line">a = numpy.array([[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">                 [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]])  <span class="comment"># 输入x, 并转换为数组</span></span><br><span class="line">b = numpy.array([[<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>],</span><br><span class="line">                 [<span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>]])  <span class="comment"># 输入y, 并转换为数组</span></span><br><span class="line"></span><br><span class="line">params = &#123;<span class="string">&quot;x&quot;</span>: a, <span class="string">&quot;y&quot;</span>: b&#125;</span><br><span class="line">outs = exe.run(fluid.default_main_program(),  <span class="comment"># 默认程序上执行</span></span><br><span class="line">               feed=params,  <span class="comment"># 喂入参数</span></span><br><span class="line">               fetch_list=[x_add_y, x_mul_y])  <span class="comment"># 获取结果</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> outs:</span><br><span class="line">    <span class="built_in">print</span>(i)</span><br></pre></td></tr></table></figure>

<h2 id="简单线性回归"><a href="#简单线性回归" class="headerlink" title="简单线性回归"></a>简单线性回归</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 简单线性回归</span></span><br><span class="line"><span class="keyword">import</span> paddle</span><br><span class="line"><span class="keyword">import</span> paddle.fluid <span class="keyword">as</span> fluid</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line">train_data = np.array([[<span class="number">0.5</span>], [<span class="number">0.6</span>], [<span class="number">0.8</span>], [<span class="number">1.1</span>], [<span class="number">1.4</span>]]).astype(<span class="string">&#x27;float32&#x27;</span>)</span><br><span class="line">y_true = np.array([[<span class="number">5.0</span>], [<span class="number">5.5</span>], [<span class="number">6.0</span>], [<span class="number">6.8</span>], [<span class="number">6.8</span>]]).astype(<span class="string">&#x27;float32&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义数据数据类型</span></span><br><span class="line">x = fluid.layers.data(name=<span class="string">&quot;x&quot;</span>, shape=[<span class="number">1</span>], dtype=<span class="string">&quot;float32&quot;</span>)</span><br><span class="line">y = fluid.layers.data(name=<span class="string">&quot;y&quot;</span>, shape=[<span class="number">1</span>], dtype=<span class="string">&quot;float32&quot;</span>)</span><br><span class="line"><span class="comment"># 通过全连接网络进行预测</span></span><br><span class="line">y_preict = fluid.layers.fc(<span class="built_in">input</span>=x, size=<span class="number">1</span>, act=<span class="literal">None</span>)</span><br><span class="line"><span class="comment"># 添加损失函数</span></span><br><span class="line">cost = fluid.layers.square_error_cost(<span class="built_in">input</span>=y_preict, label=y)</span><br><span class="line">avg_cost = fluid.layers.mean(cost)  <span class="comment"># 求均方差</span></span><br><span class="line"><span class="comment"># 定义优化方法</span></span><br><span class="line">optimizer = fluid.optimizer.SGD(learning_rate=<span class="number">0.01</span>)</span><br><span class="line">optimizer.minimize(avg_cost)  <span class="comment"># 指定最小化均方差值</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 搭建网络</span></span><br><span class="line">place = fluid.CPUPlace()  <span class="comment"># 指定在CPU执行</span></span><br><span class="line">exe = fluid.Executor(place)</span><br><span class="line">exe.run(fluid.default_startup_program())  <span class="comment"># 初始化系统参数</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 开始训练, 迭代100次</span></span><br><span class="line">costs = []</span><br><span class="line">iters = []</span><br><span class="line">values = []</span><br><span class="line">params = &#123;<span class="string">&quot;x&quot;</span>: train_data, <span class="string">&quot;y&quot;</span>: y_true&#125;</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">200</span>):</span><br><span class="line">    outs = exe.run(feed=params, fetch_list=[y_preict.name, avg_cost.name])</span><br><span class="line">    iters.append(i)  <span class="comment"># 迭代次数</span></span><br><span class="line">    costs.append(outs[<span class="number">1</span>][<span class="number">0</span>])  <span class="comment"># 损失值</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;i:&quot;</span>, i, <span class="string">&quot; cost:&quot;</span>, outs[<span class="number">1</span>][<span class="number">0</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 线性模型可视化</span></span><br><span class="line">tmp = np.random.rand(<span class="number">10</span>, <span class="number">1</span>)  <span class="comment"># 生成10行1列的均匀随机数组</span></span><br><span class="line">tmp = tmp * <span class="number">2</span>  <span class="comment"># 范围放大到0~2之间</span></span><br><span class="line">tmp.sort(axis=<span class="number">0</span>)  <span class="comment"># 排序</span></span><br><span class="line">x_test = np.array(tmp).astype(<span class="string">&quot;float32&quot;</span>)</span><br><span class="line">params = &#123;<span class="string">&quot;x&quot;</span>: x_test, <span class="string">&quot;y&quot;</span>: x_test&#125;  <span class="comment"># y参数不参加计算，只需传一个参数避免报错</span></span><br><span class="line">y_out = exe.run(feed=params, fetch_list=[y_preict.name])  <span class="comment"># 预测</span></span><br><span class="line">y_test = y_out[<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 损失函数可视化</span></span><br><span class="line">plt.figure(<span class="string">&quot;Trainging&quot;</span>)</span><br><span class="line">plt.title(<span class="string">&quot;Training Cost&quot;</span>, fontsize=<span class="number">24</span>)</span><br><span class="line">plt.xlabel(<span class="string">&quot;Iter&quot;</span>, fontsize=<span class="number">14</span>)</span><br><span class="line">plt.ylabel(<span class="string">&quot;Cost&quot;</span>, fontsize=<span class="number">14</span>)</span><br><span class="line">plt.plot(iters, costs, color=<span class="string">&quot;red&quot;</span>, label=<span class="string">&quot;Training Cost&quot;</span>)  <span class="comment"># 绘制损失函数曲线</span></span><br><span class="line">plt.grid()  <span class="comment"># 绘制网格线</span></span><br><span class="line">plt.savefig(<span class="string">&quot;train.png&quot;</span>)  <span class="comment"># 保存图片</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 线性模型可视化</span></span><br><span class="line">plt.figure(<span class="string">&quot;Inference&quot;</span>)</span><br><span class="line">plt.title(<span class="string">&quot;Linear Regression&quot;</span>, fontsize=<span class="number">24</span>)</span><br><span class="line">plt.plot(x_test, y_test, color=<span class="string">&quot;red&quot;</span>, label=<span class="string">&quot;inference&quot;</span>)  <span class="comment"># 绘制模型线条</span></span><br><span class="line">plt.scatter(train_data, y_true)  <span class="comment"># 原始样本散点图</span></span><br><span class="line"></span><br><span class="line">plt.legend()</span><br><span class="line">plt.grid()  <span class="comment"># 绘制网格线</span></span><br><span class="line">plt.savefig(<span class="string">&quot;infer.png&quot;</span>)  <span class="comment"># 保存图片</span></span><br><span class="line">plt.show()  <span class="comment"># 显示图片</span></span><br></pre></td></tr></table></figure>

<h2 id="波士顿房价预测"><a href="#波士顿房价预测" class="headerlink" title="波士顿房价预测"></a>波士顿房价预测</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 多元回归示例：波士顿房价预测</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27; 数据集介绍:</span></span><br><span class="line"><span class="string"> 1) 共506行，每行14列，前13列描述房屋特征信息，最后一列为价格中位数</span></span><br><span class="line"><span class="string"> 2) 考虑了犯罪率（CRIM）        宅用地占比（ZN）</span></span><br><span class="line"><span class="string">    非商业用地所占尺寸（INDUS）  查尔斯河虚拟变量（CHAS）</span></span><br><span class="line"><span class="string">    环保指数（NOX）            每栋住宅的房间数（RM）</span></span><br><span class="line"><span class="string">    1940年以前建成的自建单位比例（AGE）   距离5个波士顿就业中心的加权距离（DIS）</span></span><br><span class="line"><span class="string">    距离高速公路便利指数（RAD）          每一万元不动产税率（TAX）</span></span><br><span class="line"><span class="string">    教师学生比（PTRATIO）              黑人比例（B）</span></span><br><span class="line"><span class="string">    房东属于中低收入比例（LSTAT）</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="keyword">import</span> paddle</span><br><span class="line"><span class="keyword">import</span> paddle.fluid <span class="keyword">as</span> fluid</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line"><span class="comment"># step1: 数据准备</span></span><br><span class="line"><span class="comment"># paddle提供了uci_housing训练集、测试集，直接读取并返回数据</span></span><br><span class="line">BUF_SIZE = <span class="number">500</span></span><br><span class="line">BATCH_SIZE = <span class="number">20</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 训练数据集读取器</span></span><br><span class="line">random_reader = paddle.reader.shuffle(paddle.dataset.uci_housing.train(),</span><br><span class="line">                                      buf_size=BUF_SIZE)  <span class="comment"># 创建随机读取器</span></span><br><span class="line">train_reader = paddle.batch(random_reader, batch_size=BATCH_SIZE)  <span class="comment"># 训练数据读取器</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 打印数据</span></span><br><span class="line"><span class="comment">#train_data = paddle.dataset.uci_housing.train() </span></span><br><span class="line"><span class="comment">#for sample_data in train_data():</span></span><br><span class="line"><span class="comment">#    print(sample_data)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># step2: 配置网络</span></span><br><span class="line"><span class="comment"># 定义输入、输出，类型均为张量</span></span><br><span class="line">x = fluid.layers.data(name=<span class="string">&quot;x&quot;</span>, shape=[<span class="number">13</span>], dtype=<span class="string">&quot;float32&quot;</span>)</span><br><span class="line">y = fluid.layers.data(name=<span class="string">&quot;y&quot;</span>, shape=[<span class="number">1</span>], dtype=<span class="string">&quot;float32&quot;</span>)</span><br><span class="line"><span class="comment"># 定义个简单的线性网络，连接输出层、输出层</span></span><br><span class="line">y_predict = fluid.layers.fc(<span class="built_in">input</span>=x,  <span class="comment"># 输入数据</span></span><br><span class="line">                            size=<span class="number">1</span>,  <span class="comment"># 输出值个数</span></span><br><span class="line">                            act=<span class="literal">None</span>)  <span class="comment"># 激活函数</span></span><br><span class="line"><span class="comment"># 定义损失函数，并将损失函数指定给优化器</span></span><br><span class="line">cost = fluid.layers.square_error_cost(<span class="built_in">input</span>=y_predict,  <span class="comment"># 预测值，张量</span></span><br><span class="line">                                      label=y)  <span class="comment"># 期望值，张量</span></span><br><span class="line">avg_cost = fluid.layers.mean(cost)  <span class="comment"># 求损失值平均数</span></span><br><span class="line">optimizer = fluid.optimizer.SGDOptimizer(learning_rate=<span class="number">0.001</span>)  <span class="comment"># 使用随机梯度下降优化器</span></span><br><span class="line">opts = optimizer.minimize(avg_cost)  <span class="comment"># 优化器最小化损失值</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建新的program用于测试计算</span></span><br><span class="line"><span class="comment">#test_program = fluid.default_main_program().clone(for_test=True)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># step3: 模型训练、模型评估</span></span><br><span class="line">place = fluid.CPUPlace()</span><br><span class="line">exe = fluid.Executor(place)</span><br><span class="line">exe.run(fluid.default_startup_program())</span><br><span class="line"></span><br><span class="line">feeder = fluid.DataFeeder(place=place, feed_list=[x, y])</span><br><span class="line"></span><br><span class="line"><span class="built_in">iter</span> = <span class="number">0</span></span><br><span class="line">iters = []</span><br><span class="line">train_costs = []</span><br><span class="line"></span><br><span class="line">EPOCH_NUM = <span class="number">120</span></span><br><span class="line">model_save_dir = <span class="string">&quot;./model/uci_housing&quot;</span>  <span class="comment"># 模型保存路径</span></span><br><span class="line"><span class="keyword">for</span> pass_id <span class="keyword">in</span> <span class="built_in">range</span>(EPOCH_NUM):</span><br><span class="line">    train_cost = <span class="number">0</span></span><br><span class="line">    i = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> data <span class="keyword">in</span> train_reader():</span><br><span class="line">        i += <span class="number">1</span></span><br><span class="line">        train_cost = exe.run(program=fluid.default_main_program(),</span><br><span class="line">                             feed=feeder.feed(data),</span><br><span class="line">                             fetch_list=[avg_cost])</span><br><span class="line">        <span class="keyword">if</span> i % <span class="number">20</span> == <span class="number">0</span>:  <span class="comment"># 每20笔打印一次损失函数值</span></span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;PassID: %d, Cost: %0.5f&quot;</span> % (pass_id, train_cost[<span class="number">0</span>][<span class="number">0</span>]))</span><br><span class="line">        <span class="built_in">iter</span> = <span class="built_in">iter</span> + BATCH_SIZE  <span class="comment"># 加上每批次笔数</span></span><br><span class="line">        iters.append(<span class="built_in">iter</span>)  <span class="comment"># 记录笔数</span></span><br><span class="line">        train_costs.append(train_cost[<span class="number">0</span>][<span class="number">0</span>])  <span class="comment"># 记录损失值</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 保存模型</span></span><br><span class="line"><span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(model_save_dir):  <span class="comment"># 如果存储模型的目录不存在，则创建</span></span><br><span class="line">    os.makedirs(model_save_dir)</span><br><span class="line">fluid.io.save_inference_model(model_save_dir,  <span class="comment"># 保存模型的路径</span></span><br><span class="line">                              [<span class="string">&quot;x&quot;</span>],  <span class="comment"># 预测需要喂入的数据</span></span><br><span class="line">                              [y_predict],  <span class="comment"># 保存预测结果的变量</span></span><br><span class="line">                              exe)  <span class="comment"># 模型</span></span><br><span class="line"><span class="comment"># 训练过程可视化</span></span><br><span class="line">plt.figure(<span class="string">&quot;Training Cost&quot;</span>)</span><br><span class="line">plt.title(<span class="string">&quot;Training Cost&quot;</span>, fontsize=<span class="number">24</span>)</span><br><span class="line">plt.xlabel(<span class="string">&quot;iter&quot;</span>, fontsize=<span class="number">14</span>)</span><br><span class="line">plt.ylabel(<span class="string">&quot;cost&quot;</span>, fontsize=<span class="number">14</span>)</span><br><span class="line">plt.plot(iters, train_costs, color=<span class="string">&quot;red&quot;</span>, label=<span class="string">&quot;Training Cost&quot;</span>)</span><br><span class="line">plt.grid()</span><br><span class="line">plt.savefig(<span class="string">&quot;train.png&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># step4: 模型预测</span></span><br><span class="line">infer_exe = fluid.Executor(place)  <span class="comment"># 创建用于预测的Executor</span></span><br><span class="line">infer_scope = fluid.core.Scope()  <span class="comment"># 修改全局/默认作用域, 运行时中的所有变量都将分配给新的scope</span></span><br><span class="line">infer_result = [] <span class="comment">#预测值列表</span></span><br><span class="line">ground_truths = [] <span class="comment">#真实值列表</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># with fluid.scope_guard(infer_scope):</span></span><br><span class="line"><span class="comment"># 加载模型，返回三个值</span></span><br><span class="line"><span class="comment"># program: 预测程序(包含了数据、计算规则)</span></span><br><span class="line"><span class="comment"># feed_target_names: 需要喂入的变量</span></span><br><span class="line"><span class="comment"># fetch_targets: 预测结果保存的变量</span></span><br><span class="line">[infer_program, feed_target_names, fetch_targets] = \</span><br><span class="line">    fluid.io.load_inference_model(model_save_dir,  <span class="comment"># 模型保存路径</span></span><br><span class="line">                                  infer_exe)  <span class="comment"># 要执行模型的Executor</span></span><br><span class="line"><span class="comment"># 获取测试数据</span></span><br><span class="line">infer_reader = paddle.batch(paddle.dataset.uci_housing.test(),</span><br><span class="line">                            batch_size=<span class="number">200</span>)  <span class="comment"># 测试数据读取器</span></span><br><span class="line">test_data = <span class="built_in">next</span>(infer_reader())  <span class="comment"># 获取一条数据</span></span><br><span class="line">test_x = np.array([data[<span class="number">0</span>] <span class="keyword">for</span> data <span class="keyword">in</span> test_data]).astype(<span class="string">&quot;float32&quot;</span>)</span><br><span class="line">test_y = np.array([data[<span class="number">1</span>] <span class="keyword">for</span> data <span class="keyword">in</span> test_data]).astype(<span class="string">&quot;float32&quot;</span>)</span><br><span class="line"></span><br><span class="line">x_name = feed_target_names[<span class="number">0</span>]  <span class="comment"># 模型中保存的输入参数名称</span></span><br><span class="line">results = infer_exe.run(infer_program,  <span class="comment"># 预测program</span></span><br><span class="line">                        feed=&#123;x_name: np.array(test_x)&#125;,  <span class="comment"># 喂入预测的值</span></span><br><span class="line">                        fetch_list=fetch_targets)  <span class="comment"># 预测结果</span></span><br><span class="line"><span class="comment"># 预测值</span></span><br><span class="line"><span class="keyword">for</span> idx, val <span class="keyword">in</span> <span class="built_in">enumerate</span>(results[<span class="number">0</span>]):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;%d: %.2f&quot;</span> % (idx, val))</span><br><span class="line">    infer_result.append(val)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 真实值</span></span><br><span class="line"><span class="keyword">for</span> idx, val <span class="keyword">in</span> <span class="built_in">enumerate</span>(test_y):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;%d: %.2f&quot;</span> % (idx, val))</span><br><span class="line">    ground_truths.append(val)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 可视化</span></span><br><span class="line">plt.figure(<span class="string">&#x27;scatter&#x27;</span>)</span><br><span class="line">plt.title(<span class="string">&quot;TestFigure&quot;</span>, fontsize=<span class="number">24</span>)</span><br><span class="line">plt.xlabel(<span class="string">&quot;ground truth&quot;</span>, fontsize=<span class="number">14</span>)</span><br><span class="line">plt.ylabel(<span class="string">&quot;infer result&quot;</span>, fontsize=<span class="number">14</span>)</span><br><span class="line">x = np.arange(<span class="number">1</span>, <span class="number">30</span>)</span><br><span class="line">y = x</span><br><span class="line">plt.plot(x, y)</span><br><span class="line">plt.scatter(ground_truths, infer_result, color=<span class="string">&quot;green&quot;</span>, label=<span class="string">&quot;Test&quot;</span>)</span><br><span class="line">plt.grid()</span><br><span class="line">plt.legend()</span><br><span class="line">plt.savefig(<span class="string">&quot;predict.png&quot;</span>)</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>

<h2 id="增量模型训练"><a href="#增量模型训练" class="headerlink" title="增量模型训练"></a>增量模型训练</h2><p>1）模型训练与保存</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 线性回归增量训练、模型保存、固化</span></span><br><span class="line"><span class="keyword">import</span> paddle</span><br><span class="line"><span class="keyword">import</span> paddle.fluid <span class="keyword">as</span> fluid</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line">train_data = np.array([[<span class="number">0.5</span>], [<span class="number">0.6</span>], [<span class="number">0.8</span>], [<span class="number">1.1</span>], [<span class="number">1.4</span>]]).astype(<span class="string">&#x27;float32&#x27;</span>)</span><br><span class="line">y_true = np.array([[<span class="number">5.0</span>], [<span class="number">5.5</span>], [<span class="number">6.0</span>], [<span class="number">6.8</span>], [<span class="number">6.8</span>]]).astype(<span class="string">&#x27;float32&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义数据数据类型</span></span><br><span class="line">x = fluid.layers.data(name=<span class="string">&quot;x&quot;</span>, shape=[<span class="number">1</span>], dtype=<span class="string">&quot;float32&quot;</span>)</span><br><span class="line">y = fluid.layers.data(name=<span class="string">&quot;y&quot;</span>, shape=[<span class="number">1</span>], dtype=<span class="string">&quot;float32&quot;</span>)</span><br><span class="line"><span class="comment"># 通过全连接网络进行预测</span></span><br><span class="line">y_predict = fluid.layers.fc(<span class="built_in">input</span>=x, size=<span class="number">1</span>, act=<span class="literal">None</span>)</span><br><span class="line"><span class="comment"># 添加损失函数</span></span><br><span class="line">cost = fluid.layers.square_error_cost(<span class="built_in">input</span>=y_predict, label=y)</span><br><span class="line">avg_cost = fluid.layers.mean(cost)  <span class="comment"># 求均方差</span></span><br><span class="line"><span class="comment"># 定义优化方法</span></span><br><span class="line">optimizer = fluid.optimizer.SGD(learning_rate=<span class="number">0.01</span>)</span><br><span class="line">optimizer.minimize(avg_cost)  <span class="comment"># 指定最小化均方差值</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 搭建网络</span></span><br><span class="line">place = fluid.CPUPlace()  <span class="comment"># 指定在CPU执行</span></span><br><span class="line">exe = fluid.Executor(place)</span><br><span class="line">exe.run(fluid.default_startup_program())  <span class="comment"># 初始化系统参数</span></span><br><span class="line"></span><br><span class="line">model_save_dir = <span class="string">&quot;./model/lr_persis/&quot;</span></span><br><span class="line"><span class="keyword">if</span> os.path.exists(model_save_dir):</span><br><span class="line">    fluid.io.load_persistables(exe, model_save_dir, fluid.default_main_program())</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;加载增量模型成功.&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 开始迭代训练</span></span><br><span class="line">costs = []</span><br><span class="line">iters = []</span><br><span class="line">values = []</span><br><span class="line">params = &#123;<span class="string">&quot;x&quot;</span>: train_data, <span class="string">&quot;y&quot;</span>: y_true&#125;</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">50</span>):</span><br><span class="line">    outs = exe.run(feed=params, fetch_list=[y_predict.name, avg_cost.name])</span><br><span class="line">    iters.append(i)  <span class="comment"># 迭代次数</span></span><br><span class="line">    costs.append(outs[<span class="number">1</span>][<span class="number">0</span>])  <span class="comment"># 损失值</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;%d: %f&quot;</span> % (i, outs[<span class="number">1</span>][<span class="number">0</span>]))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 损失函数可视化</span></span><br><span class="line">plt.figure(<span class="string">&quot;Trainging&quot;</span>)</span><br><span class="line">plt.title(<span class="string">&quot;Training Cost&quot;</span>, fontsize=<span class="number">24</span>)</span><br><span class="line">plt.xlabel(<span class="string">&quot;Iter&quot;</span>, fontsize=<span class="number">14</span>)</span><br><span class="line">plt.ylabel(<span class="string">&quot;Cost&quot;</span>, fontsize=<span class="number">14</span>)</span><br><span class="line">plt.plot(iters, costs, color=<span class="string">&quot;red&quot;</span>, label=<span class="string">&quot;Training Cost&quot;</span>)  <span class="comment"># 绘制损失函数曲线</span></span><br><span class="line">plt.grid()  <span class="comment"># 绘制网格线</span></span><br><span class="line">plt.savefig(<span class="string">&quot;train.png&quot;</span>)  <span class="comment"># 保存图片</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">plt.legend()</span><br><span class="line">plt.grid()  <span class="comment"># 绘制网格线</span></span><br><span class="line">plt.savefig(<span class="string">&quot;infer.png&quot;</span>)  <span class="comment"># 保存图片</span></span><br><span class="line"><span class="comment"># plt.show()  # 显示图片</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;训练完成.&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 保存增量模型</span></span><br><span class="line"><span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(model_save_dir):  <span class="comment"># 如果存储模型的目录不存在，则创建</span></span><br><span class="line">    os.makedirs(model_save_dir)</span><br><span class="line">fluid.io.save_persistables(exe, model_save_dir, fluid.default_main_program())</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;保存增量模型成功.&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 保存最终模型</span></span><br><span class="line">freeze_dir = <span class="string">&quot;./model/lr_freeze/&quot;</span></span><br><span class="line"><span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(freeze_dir):  <span class="comment"># 如果存储模型的目录不存在，则创建</span></span><br><span class="line">    os.makedirs(freeze_dir)</span><br><span class="line">fluid.io.save_inference_model(freeze_dir,  <span class="comment"># 保存模型的路径</span></span><br><span class="line">                              [<span class="string">&quot;x&quot;</span>],  <span class="comment"># 预测需要喂入的数据</span></span><br><span class="line">                              [y_predict],  <span class="comment"># 保存预测结果的变量</span></span><br><span class="line">                              exe)  <span class="comment"># 模型</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;模型保存成功.&quot;</span>)</span><br></pre></td></tr></table></figure>



<p>2）模型加载与使用</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 增量模型加载</span></span><br><span class="line"><span class="keyword">import</span> paddle</span><br><span class="line"><span class="keyword">import</span> paddle.fluid <span class="keyword">as</span> fluid</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> math</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line">train_data = np.array([[<span class="number">0.5</span>], [<span class="number">0.6</span>], [<span class="number">0.8</span>], [<span class="number">1.1</span>], [<span class="number">1.4</span>]]).astype(<span class="string">&#x27;float32&#x27;</span>)</span><br><span class="line">y_true = np.array([[<span class="number">5.0</span>], [<span class="number">5.5</span>], [<span class="number">6.0</span>], [<span class="number">6.8</span>], [<span class="number">6.8</span>]]).astype(<span class="string">&#x27;float32&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 模型预测</span></span><br><span class="line">infer_exe = fluid.Executor(fluid.CPUPlace())  <span class="comment"># 创建用于预测的Executor</span></span><br><span class="line">infer_result = [] <span class="comment">#预测值列表</span></span><br><span class="line"></span><br><span class="line">freeze_dir = <span class="string">&quot;./model/lr_freeze/&quot;</span></span><br><span class="line">[infer_program, feed_target_names, fetch_targets] = \</span><br><span class="line">    fluid.io.load_inference_model(freeze_dir,  <span class="comment"># 模型保存路径</span></span><br><span class="line">                                  infer_exe)  <span class="comment"># 要执行模型的Executor</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">tmp = np.random.rand(<span class="number">10</span>, <span class="number">1</span>)  <span class="comment"># 生成10行1列的均匀随机数组</span></span><br><span class="line">tmp = tmp * <span class="number">2</span>  <span class="comment"># 范围放大到0~2之间</span></span><br><span class="line">tmp.sort(axis=<span class="number">0</span>)  <span class="comment"># 排序</span></span><br><span class="line">x_test = np.array(tmp).astype(<span class="string">&quot;float32&quot;</span>)</span><br><span class="line">x_name = feed_target_names[<span class="number">0</span>]  <span class="comment"># 模型中保存的输入参数名称</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 执行预测</span></span><br><span class="line">y_out = infer_exe.run(infer_program,  <span class="comment"># 预测program</span></span><br><span class="line">                        feed=&#123;x_name: x_test&#125;,  <span class="comment"># 喂入预测的值</span></span><br><span class="line">                        fetch_list=fetch_targets)  <span class="comment"># 预测结果</span></span><br><span class="line">y_test = y_out[<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 线性模型可视化</span></span><br><span class="line">plt.figure(<span class="string">&quot;Inference&quot;</span>)</span><br><span class="line">plt.title(<span class="string">&quot;Linear Regression&quot;</span>, fontsize=<span class="number">24</span>)</span><br><span class="line">plt.plot(x_test, y_test, color=<span class="string">&quot;red&quot;</span>, label=<span class="string">&quot;inference&quot;</span>)  <span class="comment"># 绘制模型线条</span></span><br><span class="line">plt.scatter(train_data, y_true)  <span class="comment"># 原始样本散点图</span></span><br><span class="line"></span><br><span class="line">plt.legend()</span><br><span class="line">plt.grid()  <span class="comment"># 绘制网格线</span></span><br><span class="line">plt.savefig(<span class="string">&quot;infer.png&quot;</span>)  <span class="comment"># 保存图片</span></span><br><span class="line">plt.show()  <span class="comment"># 显示图片</span></span><br></pre></td></tr></table></figure>

<p>三次增量训练效果：</p>
<p><img src= "/img/loading.gif" data-lazy-src="https://image.discover304.top/ai/dl/%E5%A2%9E%E9%87%8F%E8%AE%AD%E7%BB%83%E6%95%88%E6%9E%9C.png" alt="增量训练效果"></p>
<h2 id="水果识别"><a href="#水果识别" class="headerlink" title="水果识别"></a>水果识别</h2><ol>
<li>数据预处理部分：</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 02_fruits.py</span></span><br><span class="line"><span class="comment"># 利用深层CNN实现水果分类</span></span><br><span class="line"><span class="comment"># 数据集：爬虫从百度图片搜索结果爬取</span></span><br><span class="line"><span class="comment"># 内容：包含1036张水果图片，共5个类别（苹果288张、香蕉275张、葡萄216张、橙子276张、梨251张）</span></span><br><span class="line"></span><br><span class="line"><span class="comment">############################ 预处理部分 ################################</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line">name_dict = &#123;<span class="string">&quot;apple&quot;</span>:<span class="number">0</span>, <span class="string">&quot;banana&quot;</span>:<span class="number">1</span>, <span class="string">&quot;grape&quot;</span>:<span class="number">2</span>, <span class="string">&quot;orange&quot;</span>:<span class="number">3</span>, <span class="string">&quot;pear&quot;</span>:<span class="number">4</span>&#125;</span><br><span class="line">data_root_path = <span class="string">&quot;data/fruits/&quot;</span> <span class="comment"># 数据样本所在目录</span></span><br><span class="line">test_file_path = data_root_path + <span class="string">&quot;test.txt&quot;</span> <span class="comment">#测试文件路径</span></span><br><span class="line">train_file_path = data_root_path + <span class="string">&quot;train.txt&quot;</span> <span class="comment"># 训练文件路径</span></span><br><span class="line">name_data_list = &#123;&#125; <span class="comment"># 记录每个类别有哪些图片  key:水果名称  value:图片路径构成的列表</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将图片路径存入name_data_list字典中</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">save_train_test_file</span>(<span class="params">path, name</span>):</span><br><span class="line">    <span class="keyword">if</span> name <span class="keyword">not</span> <span class="keyword">in</span> name_data_list: <span class="comment"># 该类别水果不在字典中，则新建一个列表插入字典</span></span><br><span class="line">        img_list = []</span><br><span class="line">        img_list.append(path) <span class="comment"># 将图片路径存入列表</span></span><br><span class="line">        name_data_list[name] = img_list <span class="comment"># 将图片列表插入字典</span></span><br><span class="line">    <span class="keyword">else</span>: <span class="comment"># 该类别水果在字典中，直接添加到列表</span></span><br><span class="line">        name_data_list[name].append(path)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 遍历数据集下面每个子目录，将图片路径写入上面的字典</span></span><br><span class="line">dirs = os.listdir(data_root_path) <span class="comment"># 列出数据集目下所有的文件和子目录</span></span><br><span class="line"><span class="keyword">for</span> d <span class="keyword">in</span> dirs:</span><br><span class="line">    full_path = data_root_path + d  <span class="comment"># 拼完整路径</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> os.path.isdir(full_path): <span class="comment"># 是一个子目录</span></span><br><span class="line">        imgs = os.listdir(full_path) <span class="comment"># 列出子目录中所有的文件</span></span><br><span class="line">        <span class="keyword">for</span> img <span class="keyword">in</span> imgs:</span><br><span class="line">            save_train_test_file(full_path + <span class="string">&quot;/&quot;</span> + img, <span class="comment">#拼图片完整路径</span></span><br><span class="line">                                 d) <span class="comment"># 以子目录名称作为类别名称</span></span><br><span class="line">    <span class="keyword">else</span>: <span class="comment"># 文件</span></span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将name_data_list字典中的内容写入文件</span></span><br><span class="line"><span class="comment">## 清空训练集和测试集文件</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(test_file_path, <span class="string">&quot;w&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(train_file_path, <span class="string">&quot;w&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 遍历字典，将字典中的内容写入训练集和测试集</span></span><br><span class="line"><span class="keyword">for</span> name, img_list <span class="keyword">in</span> name_data_list.items():</span><br><span class="line">    i = <span class="number">0</span></span><br><span class="line">    num = <span class="built_in">len</span>(img_list) <span class="comment"># 获取每个类别图片数量</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;%s: %d张&quot;</span> % (name, num))</span><br><span class="line">    <span class="comment"># 写训练集和测试集</span></span><br><span class="line">    <span class="keyword">for</span> img <span class="keyword">in</span> img_list:</span><br><span class="line">        <span class="keyword">if</span> i % <span class="number">10</span> == <span class="number">0</span>: <span class="comment"># 每10笔写一笔测试集</span></span><br><span class="line">            <span class="keyword">with</span> <span class="built_in">open</span>(test_file_path, <span class="string">&quot;a&quot;</span>) <span class="keyword">as</span> f: <span class="comment">#以追加模式打开测试集文件</span></span><br><span class="line">                line = <span class="string">&quot;%s\t%d\n&quot;</span> % (img, name_dict[name]) <span class="comment"># 拼一行</span></span><br><span class="line">                f.write(line) <span class="comment"># 写入文件</span></span><br><span class="line">        <span class="keyword">else</span>: <span class="comment"># 训练集</span></span><br><span class="line">            <span class="keyword">with</span> <span class="built_in">open</span>(train_file_path, <span class="string">&quot;a&quot;</span>) <span class="keyword">as</span> f: <span class="comment">#以追加模式打开测试集文件</span></span><br><span class="line">                line = <span class="string">&quot;%s\t%d\n&quot;</span> % (img, name_dict[name]) <span class="comment"># 拼一行</span></span><br><span class="line">                f.write(line) <span class="comment"># 写入文件</span></span><br><span class="line"></span><br><span class="line">        i += <span class="number">1</span> <span class="comment"># 计数器加1</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;数据预处理完成.&quot;</span>)</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>模型训练与评估</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> paddle</span><br><span class="line"><span class="keyword">import</span> paddle.fluid <span class="keyword">as</span> fluid</span><br><span class="line"><span class="keyword">import</span> numpy</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> cpu_count</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train_mapper</span>(<span class="params">sample</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    根据传入的样本数据(一行文本)读取图片数据并返回</span></span><br><span class="line"><span class="string">    :param sample: 元组，格式为(图片路径，类别)</span></span><br><span class="line"><span class="string">    :return:返回图像数据、类别</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    img, label = sample <span class="comment"># img为路径，label为类别</span></span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(img):</span><br><span class="line">        <span class="built_in">print</span>(img, <span class="string">&quot;图片不存在&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 读取图片内容</span></span><br><span class="line">    img = paddle.dataset.image.load_image(img)</span><br><span class="line">    <span class="comment"># 对图片数据进行简单变换，设置成固定大小</span></span><br><span class="line">    img = paddle.dataset.image.simple_transform(im=img, <span class="comment"># 原始图像数据</span></span><br><span class="line">                                                resize_size=<span class="number">128</span>, <span class="comment"># 图像要设置的大小</span></span><br><span class="line">                                                crop_size=<span class="number">128</span>, <span class="comment"># 裁剪图像大小</span></span><br><span class="line">                                                is_color=<span class="literal">True</span>, <span class="comment"># 彩色图像</span></span><br><span class="line">                                                is_train=<span class="literal">True</span>) <span class="comment"># 随机裁剪</span></span><br><span class="line">    <span class="comment"># 归一化处理，将每个像素值转换到0~1</span></span><br><span class="line">    img = img.astype(<span class="string">&quot;float32&quot;</span>) / <span class="number">255.0</span></span><br><span class="line">    <span class="keyword">return</span> img, label  <span class="comment"># 返回图像、类别</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 从训练集中读取数据</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train_r</span>(<span class="params">train_list, buffered_size=<span class="number">1024</span></span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">reader</span>():</span><br><span class="line">        <span class="keyword">with</span> <span class="built_in">open</span>(train_list, <span class="string">&quot;r&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">            lines = [line.strip() <span class="keyword">for</span> line <span class="keyword">in</span> f] <span class="comment"># 读取所有行，并去空格</span></span><br><span class="line">            <span class="keyword">for</span> line <span class="keyword">in</span> lines:</span><br><span class="line">                <span class="comment"># 去掉一行数据的换行符，并按tab键拆分，存入两个变量</span></span><br><span class="line">                img_path, lab = line.replace(<span class="string">&quot;\n&quot;</span>,<span class="string">&quot;&quot;</span>).split(<span class="string">&quot;\t&quot;</span>)</span><br><span class="line">                <span class="keyword">yield</span> img_path, <span class="built_in">int</span>(lab) <span class="comment"># 返回图片路径、类别(整数)</span></span><br><span class="line">    <span class="keyword">return</span> paddle.reader.xmap_readers(train_mapper, <span class="comment"># 将reader读取的数进一步处理</span></span><br><span class="line">                                      reader, <span class="comment"># reader读取到的数据传递给train_mapper</span></span><br><span class="line">                                      cpu_count(), <span class="comment"># 线程数量</span></span><br><span class="line">                                      buffered_size) <span class="comment"># 缓冲区大小</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 搭建CNN函数</span></span><br><span class="line"><span class="comment"># 结构：输入层 --&gt; 卷积/激活/池化/dropout --&gt; 卷积/激活/池化/dropout --&gt;</span></span><br><span class="line"><span class="comment">#      卷积/激活/池化/dropout --&gt; fc --&gt; dropout --&gt; fc(softmax)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">convolution_neural_network</span>(<span class="params">image, type_size</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    创建CNN</span></span><br><span class="line"><span class="string">    :param image: 图像数据</span></span><br><span class="line"><span class="string">    :param type_size: 输出类别数量</span></span><br><span class="line"><span class="string">    :return: 分类概率</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># 第一组 卷积/激活/池化/dropout</span></span><br><span class="line">    conv_pool_1 = fluid.nets.simple_img_conv_pool(<span class="built_in">input</span>=image, <span class="comment"># 原始图像数据</span></span><br><span class="line">                                                  filter_size=<span class="number">3</span>, <span class="comment"># 卷积核大小</span></span><br><span class="line">                                                  num_filters=<span class="number">32</span>, <span class="comment"># 卷积核数量</span></span><br><span class="line">                                                  pool_size=<span class="number">2</span>, <span class="comment"># 2*2区域池化</span></span><br><span class="line">                                                  pool_stride=<span class="number">2</span>, <span class="comment"># 池化步长值</span></span><br><span class="line">                                                  act=<span class="string">&quot;relu&quot;</span>)<span class="comment">#激活函数</span></span><br><span class="line">    drop = fluid.layers.dropout(x=conv_pool_1, dropout_prob=<span class="number">0.5</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 第二组</span></span><br><span class="line">    conv_pool_2 = fluid.nets.simple_img_conv_pool(<span class="built_in">input</span>=drop, <span class="comment"># 以上一个drop输出作为输入</span></span><br><span class="line">                                                  filter_size=<span class="number">3</span>, <span class="comment"># 卷积核大小</span></span><br><span class="line">                                                  num_filters=<span class="number">64</span>, <span class="comment"># 卷积核数量</span></span><br><span class="line">                                                  pool_size=<span class="number">2</span>, <span class="comment"># 2*2区域池化</span></span><br><span class="line">                                                  pool_stride=<span class="number">2</span>, <span class="comment"># 池化步长值</span></span><br><span class="line">                                                  act=<span class="string">&quot;relu&quot;</span>)<span class="comment">#激活函数</span></span><br><span class="line">    drop = fluid.layers.dropout(x=conv_pool_2, dropout_prob=<span class="number">0.5</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 第三组</span></span><br><span class="line">    conv_pool_3 = fluid.nets.simple_img_conv_pool(<span class="built_in">input</span>=drop, <span class="comment"># 以上一个drop输出作为输入</span></span><br><span class="line">                                                  filter_size=<span class="number">3</span>, <span class="comment"># 卷积核大小</span></span><br><span class="line">                                                  num_filters=<span class="number">64</span>, <span class="comment"># 卷积核数量</span></span><br><span class="line">                                                  pool_size=<span class="number">2</span>, <span class="comment"># 2*2区域池化</span></span><br><span class="line">                                                  pool_stride=<span class="number">2</span>, <span class="comment"># 池化步长值</span></span><br><span class="line">                                                  act=<span class="string">&quot;relu&quot;</span>)<span class="comment">#激活函数</span></span><br><span class="line">    drop = fluid.layers.dropout(x=conv_pool_3, dropout_prob=<span class="number">0.5</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 全连接层</span></span><br><span class="line">    fc = fluid.layers.fc(<span class="built_in">input</span>=drop, size=<span class="number">512</span>, act=<span class="string">&quot;relu&quot;</span>)</span><br><span class="line">    <span class="comment"># dropout</span></span><br><span class="line">    drop = fluid.layers.dropout(x=fc, dropout_prob=<span class="number">0.5</span>)</span><br><span class="line">    <span class="comment"># 输出层(fc)</span></span><br><span class="line">    predict = fluid.layers.fc(<span class="built_in">input</span>=drop, <span class="comment"># 输入</span></span><br><span class="line">                              size=type_size, <span class="comment"># 输出值的个数(5个类别)</span></span><br><span class="line">                              act=<span class="string">&quot;softmax&quot;</span>) <span class="comment"># 输出层采用softmax作为激活函数</span></span><br><span class="line">    <span class="keyword">return</span> predict</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义reader</span></span><br><span class="line">BATCH_SIZE = <span class="number">32</span>  <span class="comment"># 批次大小</span></span><br><span class="line">trainer_reader = train_r(train_list=train_file_path) <span class="comment">#原始reader</span></span><br><span class="line">random_train_reader = paddle.reader.shuffle(reader=trainer_reader,</span><br><span class="line">                                            buf_size=<span class="number">1300</span>) <span class="comment"># 包装成随机读取器</span></span><br><span class="line">batch_train_reader = paddle.batch(random_train_reader,</span><br><span class="line">                                  batch_size=BATCH_SIZE) <span class="comment"># 批量读取器</span></span><br><span class="line"><span class="comment"># 变量</span></span><br><span class="line">image = fluid.layers.data(name=<span class="string">&quot;image&quot;</span>, shape=[<span class="number">3</span>, <span class="number">128</span>, <span class="number">128</span>], dtype=<span class="string">&quot;float32&quot;</span>)</span><br><span class="line">label = fluid.layers.data(name=<span class="string">&quot;label&quot;</span>, shape=[<span class="number">1</span>], dtype=<span class="string">&quot;int64&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用函数，创建CNN</span></span><br><span class="line">predict = convolution_neural_network(image=image, type_size=<span class="number">5</span>)</span><br><span class="line"><span class="comment"># 损失函数:交叉熵</span></span><br><span class="line">cost = fluid.layers.cross_entropy(<span class="built_in">input</span>=predict, <span class="comment"># 预测结果</span></span><br><span class="line">                                  label=label) <span class="comment"># 真实结果</span></span><br><span class="line">avg_cost = fluid.layers.mean(cost)</span><br><span class="line"><span class="comment"># 计算准确率</span></span><br><span class="line">accuracy = fluid.layers.accuracy(<span class="built_in">input</span>=predict, <span class="comment"># 预测结果</span></span><br><span class="line">                                label=label) <span class="comment"># 真实结果</span></span><br><span class="line"><span class="comment"># 优化器</span></span><br><span class="line">optimizer = fluid.optimizer.Adam(learning_rate=<span class="number">0.001</span>)</span><br><span class="line">optimizer.minimize(avg_cost) <span class="comment"># 将损失函数值优化到最小</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 执行器</span></span><br><span class="line"><span class="comment"># place = fluid.CPUPlace()</span></span><br><span class="line">place = fluid.CUDAPlace(<span class="number">0</span>) <span class="comment"># GPU训练</span></span><br><span class="line">exe = fluid.Executor(place)</span><br><span class="line">exe.run(fluid.default_startup_program())</span><br><span class="line"><span class="comment"># feeder</span></span><br><span class="line">feeder = fluid.DataFeeder(feed_list=[image, label],  <span class="comment"># 指定要喂入数据</span></span><br><span class="line">                          place=place)</span><br><span class="line"></span><br><span class="line">model_save_dir = <span class="string">&quot;model/fruits/&quot;</span> <span class="comment"># 模型保存路径</span></span><br><span class="line">costs = [] <span class="comment"># 记录损失值</span></span><br><span class="line">accs = [] <span class="comment"># 记录准确度</span></span><br><span class="line">times = <span class="number">0</span></span><br><span class="line">batches = [] <span class="comment"># 迭代次数</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 开始训练</span></span><br><span class="line"><span class="keyword">for</span> pass_id <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">40</span>):</span><br><span class="line">    train_cost = <span class="number">0</span> <span class="comment"># 临时变量，记录每次训练的损失值</span></span><br><span class="line">    <span class="keyword">for</span> batch_id, data <span class="keyword">in</span> <span class="built_in">enumerate</span>(batch_train_reader()): <span class="comment"># 循环读取样本，执行训练</span></span><br><span class="line">        times += <span class="number">1</span></span><br><span class="line">        train_cost, train_acc = exe.run(program=fluid.default_main_program(),</span><br><span class="line">                                        feed=feeder.feed(data), <span class="comment"># 喂入参数</span></span><br><span class="line">                                        fetch_list=[avg_cost, accuracy])<span class="comment"># 获取损失值、准确率</span></span><br><span class="line">        <span class="keyword">if</span> batch_id % <span class="number">20</span> == <span class="number">0</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;pass_id:%d, step:%d, cost:%f, acc:%f&quot;</span> %</span><br><span class="line">                  (pass_id, batch_id, train_cost[<span class="number">0</span>], train_acc[<span class="number">0</span>]))</span><br><span class="line">            accs.append(train_acc[<span class="number">0</span>]) <span class="comment"># 记录准确率</span></span><br><span class="line">            costs.append(train_cost[<span class="number">0</span>]) <span class="comment"># 记录损失值</span></span><br><span class="line">            batches.append(times) <span class="comment"># 记录迭代次数</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 训练结束后，保存模型</span></span><br><span class="line"><span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(model_save_dir):</span><br><span class="line">    os.makedirs(model_save_dir)</span><br><span class="line">fluid.io.save_inference_model(dirname=model_save_dir,</span><br><span class="line">                              feeded_var_names=[<span class="string">&quot;image&quot;</span>],</span><br><span class="line">                              target_vars=[predict],</span><br><span class="line">                              executor=exe)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;训练保存模型完成!&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 训练过程可视化</span></span><br><span class="line">plt.title(<span class="string">&quot;training&quot;</span>, fontsize=<span class="number">24</span>)</span><br><span class="line">plt.xlabel(<span class="string">&quot;iter&quot;</span>, fontsize=<span class="number">20</span>)</span><br><span class="line">plt.ylabel(<span class="string">&quot;cost/acc&quot;</span>, fontsize=<span class="number">20</span>)</span><br><span class="line">plt.plot(batches, costs, color=<span class="string">&#x27;red&#x27;</span>, label=<span class="string">&quot;Training Cost&quot;</span>)</span><br><span class="line">plt.plot(batches, accs, color=<span class="string">&#x27;green&#x27;</span>, label=<span class="string">&quot;Training Acc&quot;</span>)</span><br><span class="line">plt.legend()</span><br><span class="line">plt.grid()</span><br><span class="line">plt.savefig(<span class="string">&quot;train.png&quot;</span>)</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>预测</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义执行器</span></span><br><span class="line">place = fluid.CPUPlace()</span><br><span class="line">infer_exe = fluid.Executor(place)</span><br><span class="line">model_save_dir = <span class="string">&quot;model/fruits/&quot;</span> <span class="comment"># 模型保存路径</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 加载数据</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">load_img</span>(<span class="params">path</span>):</span><br><span class="line">    img = paddle.dataset.image.load_and_transform(path, <span class="number">128</span>, <span class="number">128</span>, <span class="literal">False</span>).astype(<span class="string">&quot;float32&quot;</span>)</span><br><span class="line">    img = img / <span class="number">255.0</span></span><br><span class="line">    <span class="keyword">return</span> img</span><br><span class="line"></span><br><span class="line">infer_imgs = [] <span class="comment"># 存放要预测图像数据</span></span><br><span class="line">test_img = <span class="string">&quot;./data/grape_1.png&quot;</span> <span class="comment">#待预测图片</span></span><br><span class="line">infer_imgs.append(load_img(test_img)) <span class="comment">#加载图片，并且将图片数据添加到待预测列表</span></span><br><span class="line">infer_imgs = numpy.array(infer_imgs) <span class="comment"># 转换成数组</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 加载模型</span></span><br><span class="line">infer_program, feed_target_names, fetch_targets = \</span><br><span class="line">    fluid.io.load_inference_model(model_save_dir, infer_exe)</span><br><span class="line"><span class="comment"># 执行预测</span></span><br><span class="line">results = infer_exe.run(infer_program, <span class="comment"># 执行预测program</span></span><br><span class="line">                        feed=&#123;feed_target_names[<span class="number">0</span>]: infer_imgs&#125;, <span class="comment"># 传入待预测图像数据</span></span><br><span class="line">                        fetch_list=fetch_targets) <span class="comment">#返回结果</span></span><br><span class="line"><span class="built_in">print</span>(results)</span><br><span class="line"></span><br><span class="line">result = numpy.argmax(results[<span class="number">0</span>]) <span class="comment"># 取出预测结果中概率最大的元素索引值</span></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> name_dict.items(): <span class="comment"># 将类别由数字转换为名称</span></span><br><span class="line">    <span class="keyword">if</span> result == v:  <span class="comment"># 如果预测结果等于v, 打印出名称</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;预测结果:&quot;</span>, k) <span class="comment"># 打印出名称</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 显示待预测的图片</span></span><br><span class="line">img = Image.<span class="built_in">open</span>(test_img)</span><br><span class="line">plt.imshow(img)</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
<h2 id="利用VGG实现图像分类"><a href="#利用VGG实现图像分类" class="headerlink" title="利用VGG实现图像分类"></a>利用VGG实现图像分类</h2><p>在水果识别案例第二部分（模型搭建部分）增加如下代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建VGG模型</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">vgg_bn_drop</span>(<span class="params">image, type_size</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">conv_block</span>(<span class="params">ipt, num_filter, groups, dropouts</span>):</span><br><span class="line">        <span class="comment"># 创建Convolution2d, BatchNorm, DropOut, Pool2d组</span></span><br><span class="line">        <span class="keyword">return</span> fluid.nets.img_conv_group(<span class="built_in">input</span>=ipt, <span class="comment"># 输入图像像，[N,C,H,W]格式</span></span><br><span class="line">                                         pool_stride=<span class="number">2</span>, <span class="comment"># 池化步长值</span></span><br><span class="line">                                         pool_size=<span class="number">2</span>, <span class="comment"># 池化区域大小</span></span><br><span class="line">                                         conv_num_filter=[num_filter] * groups, <span class="comment">#卷积核数量</span></span><br><span class="line">                                         conv_filter_size=<span class="number">3</span>, <span class="comment"># 卷积核大小</span></span><br><span class="line">                                         conv_act=<span class="string">&quot;relu&quot;</span>, <span class="comment"># 激活函数</span></span><br><span class="line">                                         conv_with_batchnorm=<span class="literal">True</span>,<span class="comment">#是否使用batch normal</span></span><br><span class="line">                                         pool_type=<span class="string">&quot;max&quot;</span>) <span class="comment"># 池化类型</span></span><br><span class="line">    conv1 = conv_block(image, <span class="number">64</span>, <span class="number">2</span>, [<span class="number">0.0</span>, <span class="number">0</span>]) <span class="comment"># 最后一个参数个数和组数量相对应</span></span><br><span class="line">    conv2 = conv_block(conv1, <span class="number">128</span>, <span class="number">2</span>, [<span class="number">0.0</span>, <span class="number">0</span>])</span><br><span class="line">    conv3 = conv_block(conv2, <span class="number">256</span>, <span class="number">3</span>, [<span class="number">0.0</span>, <span class="number">0.0</span>, <span class="number">0.0</span>])</span><br><span class="line">    conv4 = conv_block(conv3, <span class="number">512</span>, <span class="number">3</span>, [<span class="number">0.0</span>, <span class="number">0.0</span>, <span class="number">0.0</span>])</span><br><span class="line">    conv5 = conv_block(conv4, <span class="number">512</span>, <span class="number">3</span>, [<span class="number">0.0</span>, <span class="number">0.0</span>, <span class="number">0.0</span>])</span><br><span class="line"></span><br><span class="line">    drop = fluid.layers.dropout(x=conv5, dropout_prob=<span class="number">0.2</span>) <span class="comment"># 待调整</span></span><br><span class="line">    fc1 = fluid.layers.fc(<span class="built_in">input</span>=drop, size=<span class="number">512</span>, act=<span class="literal">None</span>)</span><br><span class="line"></span><br><span class="line">    bn = fluid.layers.batch_norm(<span class="built_in">input</span>=fc1, act=<span class="string">&quot;relu&quot;</span>) <span class="comment"># batch normal</span></span><br><span class="line">    drop2 = fluid.layers.dropout(x=bn, dropout_prob=<span class="number">0.0</span>)</span><br><span class="line">    fc2 = fluid.layers.fc(<span class="built_in">input</span>=drop2, size=<span class="number">512</span>, act=<span class="literal">None</span>)</span><br><span class="line">    predict = fluid.layers.fc(<span class="built_in">input</span>=fc2, size=type_size, act=<span class="string">&quot;softmax&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> predict</span><br></pre></td></tr></table></figure>

<p>将创建网络部分改为以下代码即可：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 调用上面的函数创建VGG</span></span><br><span class="line">predict = vgg_bn_drop(image=image, type_size=<span class="number">5</span>) <span class="comment"># type_size和水果类别一致</span></span><br></pre></td></tr></table></figure>

<h2 id="中文文本分类"><a href="#中文文本分类" class="headerlink" title="中文文本分类"></a>中文文本分类</h2><ol>
<li>数据预处</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 中文资讯分类示例</span></span><br><span class="line"><span class="comment"># 任务：根据样本，训练模型，将新的文本划分到正确的类别</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">数据来源：从网站上爬取56821条中文新闻摘要</span></span><br><span class="line"><span class="string">数据类容：包含10类(国际、文化、娱乐、体育、财经、汽车、教育、科技、房产、证券)</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">############################# 数据预处理 ##############################</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> cpu_count</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> paddle</span><br><span class="line"><span class="keyword">import</span> paddle.fluid <span class="keyword">as</span> fluid</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义公共变量</span></span><br><span class="line">data_root = <span class="string">&quot;data/news_classify/&quot;</span> <span class="comment"># 数据集所在目录</span></span><br><span class="line">data_file = <span class="string">&quot;news_classify_data.txt&quot;</span> <span class="comment"># 原始样本文件名</span></span><br><span class="line">test_file = <span class="string">&quot;test_list.txt&quot;</span> <span class="comment"># 测试集文件名称</span></span><br><span class="line">train_file = <span class="string">&quot;train_list.txt&quot;</span> <span class="comment"># 训练集文件名称</span></span><br><span class="line">dict_file = <span class="string">&quot;dict_txt.txt&quot;</span> <span class="comment"># 编码后的字典文件</span></span><br><span class="line"></span><br><span class="line">data_file_path = data_root + data_file <span class="comment"># 样本文件完整路径</span></span><br><span class="line">dict_file_path = data_root + dict_file <span class="comment"># 字典文件完整路径</span></span><br><span class="line">test_file_path = data_root + test_file <span class="comment"># 测试集文件完整路径</span></span><br><span class="line">train_file_path = data_root + train_file <span class="comment"># 训练集文件完整路径</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成字典文件：把每个字编码成一个数字，并存入文件中</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">create_dict</span>():</span><br><span class="line">    dict_set = <span class="built_in">set</span>()  <span class="comment"># 集合，去重</span></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(data_file_path, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f: <span class="comment"># 打开原始样本文件</span></span><br><span class="line">        lines = f.readlines() <span class="comment"># 读取所有的行</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 遍历每行</span></span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> lines:</span><br><span class="line">        title = line.split(<span class="string">&quot;_!_&quot;</span>)[-<span class="number">1</span>].replace(<span class="string">&quot;\n&quot;</span>, <span class="string">&quot;&quot;</span>) <span class="comment">#取出标题部分，并取出换行符</span></span><br><span class="line">        <span class="keyword">for</span> w <span class="keyword">in</span> title: <span class="comment"># 取出标题部分每个字</span></span><br><span class="line">            dict_set.add(w) <span class="comment"># 将每个字存入集合进行去重</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 遍历集合，每个字分配一个编码</span></span><br><span class="line">    dict_list = []</span><br><span class="line">    i = <span class="number">0</span> <span class="comment"># 计数器</span></span><br><span class="line">    <span class="keyword">for</span> s <span class="keyword">in</span> dict_set:</span><br><span class="line">        dict_list.append([s, i]) <span class="comment"># 将&quot;文字,编码&quot;键值对添加到列表中</span></span><br><span class="line">        i += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">    dict_txt = <span class="built_in">dict</span>(dict_list) <span class="comment"># 将列表转换为字典</span></span><br><span class="line">    end_dict = &#123;<span class="string">&quot;&lt;unk&gt;&quot;</span>: i&#125; <span class="comment"># 未知字符</span></span><br><span class="line">    dict_txt.update(end_dict) <span class="comment"># 将未知字符编码添加到字典中</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 将字典保存到文件中</span></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(dict_file_path, <span class="string">&quot;w&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        f.write(<span class="built_in">str</span>(dict_txt))  <span class="comment"># 将字典转换为字符串并存入文件</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;生成字典完成.&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 对一行标题进行编码</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">line_encoding</span>(<span class="params">title, dict_txt, label</span>):</span><br><span class="line">    new_line = <span class="string">&quot;&quot;</span>  <span class="comment"># 返回的结果</span></span><br><span class="line">    <span class="keyword">for</span> w <span class="keyword">in</span> title:</span><br><span class="line">        <span class="keyword">if</span> w <span class="keyword">in</span> dict_txt: <span class="comment"># 如果字已经在字典中</span></span><br><span class="line">            code = <span class="built_in">str</span>(dict_txt[w])  <span class="comment"># 取出对应的编码</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            code = <span class="built_in">str</span>(dict_txt[<span class="string">&quot;&lt;unk&gt;&quot;</span>]) <span class="comment"># 取未知字符的编码</span></span><br><span class="line">        new_line = new_line + code + <span class="string">&quot;,&quot;</span> <span class="comment"># 将编码追加到新的字符串后</span></span><br><span class="line"></span><br><span class="line">    new_line = new_line[:-<span class="number">1</span>] <span class="comment"># 去掉最后一个逗号</span></span><br><span class="line">    new_line = new_line + <span class="string">&quot;\t&quot;</span> + label + <span class="string">&quot;\n&quot;</span> <span class="comment"># 拼接成一行，标题和标签用\t分隔</span></span><br><span class="line">    <span class="keyword">return</span> new_line</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 对原始样本进行编码，对每个标题的每个字使用字典中编码的整数进行替换</span></span><br><span class="line"><span class="comment"># 产生编码后的句子，并且存入测试集、训练集</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">create_data_list</span>():</span><br><span class="line">    <span class="comment"># 清空测试集、训练集文件</span></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(test_file_path, <span class="string">&quot;w&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(train_file_path, <span class="string">&quot;w&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 打开原始样本文件，取出标题部分，对标题进行编码</span></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(dict_file_path, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f_dict:</span><br><span class="line">        <span class="comment"># 读取字典文件中的第一行(只有一行)，通过调用eval函数转换为字典对象</span></span><br><span class="line">        dict_txt = <span class="built_in">eval</span>(f_dict.readlines()[<span class="number">0</span>])</span><br><span class="line"></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(data_file_path, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f_data:</span><br><span class="line">        lines = f_data.readlines()</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 取出标题并编码</span></span><br><span class="line">    i = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> lines:</span><br><span class="line">        words = line.replace(<span class="string">&quot;\n&quot;</span>, <span class="string">&quot;&quot;</span>).split(<span class="string">&quot;_!_&quot;</span>) <span class="comment"># 拆分每行</span></span><br><span class="line">        label = words[<span class="number">1</span>] <span class="comment"># 分类</span></span><br><span class="line">        title = words[<span class="number">3</span>] <span class="comment"># 标题</span></span><br><span class="line"></span><br><span class="line">        new_line = line_encoding(title, dict_txt, label)  <span class="comment"># 对标题进行编码</span></span><br><span class="line">        <span class="keyword">if</span> i % <span class="number">10</span> == <span class="number">0</span>: <span class="comment"># 每10笔写一笔测试集文件</span></span><br><span class="line">            <span class="keyword">with</span> <span class="built_in">open</span>(test_file_path, <span class="string">&quot;a&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                f.write(new_line)</span><br><span class="line">        <span class="keyword">else</span>: <span class="comment"># 写入训练集</span></span><br><span class="line">            <span class="keyword">with</span> <span class="built_in">open</span>(train_file_path, <span class="string">&quot;a&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                f.write(new_line)</span><br><span class="line">        i += <span class="number">1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;生成测试集、训练集结束.&quot;</span>)</span><br><span class="line"></span><br><span class="line">create_dict()  <span class="comment"># 生成字典</span></span><br><span class="line">create_data_list() <span class="comment"># 生成训练集、测试集</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>模型训练与评估</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 读取字典文件，并返回字典长度</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_dict_len</span>(<span class="params">dict_path</span>):</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(dict_path, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        line = <span class="built_in">eval</span>(f.readlines()[<span class="number">0</span>])  <span class="comment"># 读取字典文件内容，并返回一个字典对象</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">len</span>(line.keys())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义data_mapper，将reader读取的数据进行二次处理</span></span><br><span class="line"><span class="comment"># 将传入的字符串转换为整型并返回</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">data_mapper</span>(<span class="params">sample</span>):</span><br><span class="line">    data, label = sample  <span class="comment"># 将sample元组拆分到两个变量</span></span><br><span class="line">    <span class="comment"># 拆分句子，将每个编码转换为数字, 并存入一个列表中</span></span><br><span class="line">    val = [<span class="built_in">int</span>(w) <span class="keyword">for</span> w <span class="keyword">in</span> data.split(<span class="string">&quot;,&quot;</span>)]</span><br><span class="line">    <span class="keyword">return</span> val, <span class="built_in">int</span>(label)  <span class="comment"># 返回整数列表，标签(转换成整数)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义reader</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train_reader</span>(<span class="params">train_file_path</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">reader</span>():</span><br><span class="line">        <span class="keyword">with</span> <span class="built_in">open</span>(train_file_path, <span class="string">&quot;r&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">            lines = f.readlines()  <span class="comment"># 读取所有的行</span></span><br><span class="line">            np.random.shuffle(lines)  <span class="comment"># 打乱所有样本</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> line <span class="keyword">in</span> lines:</span><br><span class="line">                data, label = line.split(<span class="string">&quot;\t&quot;</span>)  <span class="comment"># 拆分样本到两个变量中</span></span><br><span class="line">                <span class="keyword">yield</span> data, label</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> paddle.reader.xmap_readers(data_mapper,  <span class="comment"># reader读取的数据进行下一步处理函数</span></span><br><span class="line">                                      reader,  <span class="comment"># 读取样本的reader</span></span><br><span class="line">                                      cpu_count(),  <span class="comment"># 线程数</span></span><br><span class="line">                                      <span class="number">1024</span>)  <span class="comment"># 缓冲区大小</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 读取测试集reader</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">test_reader</span>(<span class="params">test_file_path</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">reader</span>():</span><br><span class="line">        <span class="keyword">with</span> <span class="built_in">open</span>(test_file_path, <span class="string">&quot;r&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">            lines = f.readlines()</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> line <span class="keyword">in</span> lines:</span><br><span class="line">                data, label = line.split(<span class="string">&quot;\t&quot;</span>)</span><br><span class="line">                <span class="keyword">yield</span> data, label</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> paddle.reader.xmap_readers(data_mapper,</span><br><span class="line">                                      reader,</span><br><span class="line">                                      cpu_count(),</span><br><span class="line">                                      <span class="number">1024</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义网络</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">CNN_net</span>(<span class="params">data, dict_dim, class_dim=<span class="number">10</span>, emb_dim=<span class="number">128</span>, hid_dim=<span class="number">128</span>, hid_dim2=<span class="number">98</span></span>):</span><br><span class="line">    <span class="comment"># embedding(词嵌入层)：生成词向量，得到一个新的粘稠的实向量</span></span><br><span class="line">    <span class="comment"># 以使用较少的维度，表达更丰富的信息</span></span><br><span class="line">    emb = fluid.layers.embedding(<span class="built_in">input</span>=data, size=[dict_dim, emb_dim])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 并列两个卷积、池化层</span></span><br><span class="line">    conv1 = fluid.nets.sequence_conv_pool(<span class="built_in">input</span>=emb,  <span class="comment"># 输入，上一个词嵌入层的输出作为输入</span></span><br><span class="line">                                          num_filters=hid_dim,  <span class="comment"># 卷积核数量</span></span><br><span class="line">                                          filter_size=<span class="number">3</span>,  <span class="comment"># 卷积核大小</span></span><br><span class="line">                                          act=<span class="string">&quot;tanh&quot;</span>,  <span class="comment"># 激活函数</span></span><br><span class="line">                                          pool_type=<span class="string">&quot;sqrt&quot;</span>)  <span class="comment"># 池化类型</span></span><br><span class="line"></span><br><span class="line">    conv2 = fluid.nets.sequence_conv_pool(<span class="built_in">input</span>=emb,  <span class="comment"># 输入，上一个词嵌入层的输出作为输入</span></span><br><span class="line">                                          num_filters=hid_dim2,  <span class="comment"># 卷积核数量</span></span><br><span class="line">                                          filter_size=<span class="number">4</span>,  <span class="comment"># 卷积核大小</span></span><br><span class="line">                                          act=<span class="string">&quot;tanh&quot;</span>,  <span class="comment"># 激活函数</span></span><br><span class="line">                                          pool_type=<span class="string">&quot;sqrt&quot;</span>)  <span class="comment"># 池化类型</span></span><br><span class="line">    output = fluid.layers.fc(<span class="built_in">input</span>=[conv1, conv2],  <span class="comment"># 输入</span></span><br><span class="line">                             size=class_dim,  <span class="comment"># 输出类别数量</span></span><br><span class="line">                             act=<span class="string">&quot;softmax&quot;</span>)  <span class="comment"># 激活函数</span></span><br><span class="line">    <span class="keyword">return</span> output</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义模型、训练、评估、保存</span></span><br><span class="line">model_save_dir = <span class="string">&quot;model/news_classify/&quot;</span>  <span class="comment"># 模型保存路径</span></span><br><span class="line"></span><br><span class="line">words = fluid.layers.data(name=<span class="string">&quot;words&quot;</span>, shape=[<span class="number">1</span>], dtype=<span class="string">&quot;int64&quot;</span>,</span><br><span class="line">                          lod_level=<span class="number">1</span>) <span class="comment"># 张量层级</span></span><br><span class="line">label = fluid.layers.data(name=<span class="string">&quot;label&quot;</span>, shape=[<span class="number">1</span>], dtype=<span class="string">&quot;int64&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取字典长度</span></span><br><span class="line">dict_dim = get_dict_len(dict_file_path)</span><br><span class="line"><span class="comment"># 调用函数创建CNN</span></span><br><span class="line">model = CNN_net(words, dict_dim)</span><br><span class="line"><span class="comment"># 定义损失函数</span></span><br><span class="line">cost = fluid.layers.cross_entropy(<span class="built_in">input</span>=model, <span class="comment"># 预测结果</span></span><br><span class="line">                                  label=label) <span class="comment"># 真实结果</span></span><br><span class="line">avg_cost = fluid.layers.mean(cost) <span class="comment"># 求损失函数均值</span></span><br><span class="line"><span class="comment"># 准确率</span></span><br><span class="line">acc = fluid.layers.accuracy(<span class="built_in">input</span>=model, <span class="comment"># 预测结果</span></span><br><span class="line">                            label=label) <span class="comment"># 真实结果</span></span><br><span class="line"><span class="comment"># 克隆program用于模型测试评估</span></span><br><span class="line"><span class="comment"># for_test如果为True，会少一些优化</span></span><br><span class="line">test_program = fluid.default_main_program().clone(for_test=<span class="literal">True</span>)</span><br><span class="line"><span class="comment"># 定义优化器</span></span><br><span class="line">optimizer = fluid.optimizer.AdagradOptimizer(learning_rate=<span class="number">0.001</span>)</span><br><span class="line">optimizer.minimize(avg_cost)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义执行器</span></span><br><span class="line">place = fluid.CPUPlace()</span><br><span class="line">exe = fluid.Executor(place)</span><br><span class="line">exe.run(fluid.default_startup_program())</span><br><span class="line"></span><br><span class="line"><span class="comment"># 准备数据</span></span><br><span class="line">tr_reader = train_reader(train_file_path)</span><br><span class="line">batch_train_reader = paddle.batch(reader=tr_reader, batch_size=<span class="number">128</span>)</span><br><span class="line"></span><br><span class="line">ts_reader = test_reader(test_file_path)</span><br><span class="line">batch_test_reader = paddle.batch(reader=ts_reader, batch_size=<span class="number">128</span>)</span><br><span class="line"></span><br><span class="line">feeder = fluid.DataFeeder(place=place, feed_list=[words, label]) <span class="comment"># feeder</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 开始训练</span></span><br><span class="line"><span class="keyword">for</span> pass_id <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">20</span>):</span><br><span class="line">    <span class="keyword">for</span> batch_id, data <span class="keyword">in</span> <span class="built_in">enumerate</span>(batch_train_reader()):</span><br><span class="line">        train_cost, train_acc = exe.run(program=fluid.default_main_program(),</span><br><span class="line">                                        feed=feeder.feed(data), <span class="comment"># 喂入数据</span></span><br><span class="line">                                        fetch_list=[avg_cost, acc]) <span class="comment"># 要获取的结果</span></span><br><span class="line">        <span class="comment"># 打印</span></span><br><span class="line">        <span class="keyword">if</span> batch_id % <span class="number">100</span> == <span class="number">0</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;pass_id:%d, batch_id:%d, cost:%f, acc:%f&quot;</span> %</span><br><span class="line">                  (pass_id, batch_id, train_cost[<span class="number">0</span>], train_acc[<span class="number">0</span>]))</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 每轮次训练完成后，进行模型评估</span></span><br><span class="line">    test_costs_list = [] <span class="comment"># 存放所有的损失值</span></span><br><span class="line">    test_accs_list = [] <span class="comment"># 存放准确率</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> batch_id, data <span class="keyword">in</span> <span class="built_in">enumerate</span>(batch_test_reader()):  <span class="comment"># 读取一个批次测试数据</span></span><br><span class="line">        test_cost, test_acc = exe.run(program=test_program, <span class="comment"># 执行test_program</span></span><br><span class="line">                                      feed=feeder.feed(data), <span class="comment"># 喂入测试数据</span></span><br><span class="line">                                      fetch_list=[avg_cost, acc])  <span class="comment"># 要获取的结果</span></span><br><span class="line">        test_costs_list.append(test_cost[<span class="number">0</span>]) <span class="comment"># 记录损失值</span></span><br><span class="line">        test_accs_list.append(test_acc[<span class="number">0</span>]) <span class="comment"># 记录准确率</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 计算平均准确率和损失值</span></span><br><span class="line">    avg_test_cost = <span class="built_in">sum</span>(test_costs_list) / <span class="built_in">len</span>(test_costs_list)</span><br><span class="line">    avg_test_acc = <span class="built_in">sum</span>(test_accs_list) / <span class="built_in">len</span>(test_accs_list)</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;pass_id:%d, test_cost:%f, test_acc:%f&quot;</span> %</span><br><span class="line">          (pass_id, avg_test_cost, avg_test_acc))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 保存模型</span></span><br><span class="line"><span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(model_save_dir):</span><br><span class="line">    os.makedirs(model_save_dir)</span><br><span class="line">fluid.io.save_inference_model(model_save_dir, <span class="comment"># 模型保存路径</span></span><br><span class="line">                              feeded_var_names=[words.name], <span class="comment"># 使用模型时需传入的参数</span></span><br><span class="line">                              target_vars=[model], <span class="comment"># 预测结果</span></span><br><span class="line">                              executor=exe) <span class="comment"># 执行器</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;模型保存完成.&quot;</span>)</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>预测</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">model_save_dir = <span class="string">&quot;model/news_classify/&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_data</span>(<span class="params">sentence</span>):</span><br><span class="line">    <span class="comment"># 读取字典中的内容</span></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(dict_file_path, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&quot;utf-8&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        dict_txt = <span class="built_in">eval</span>(f.readlines()[<span class="number">0</span>])</span><br><span class="line"></span><br><span class="line">    keys = dict_txt.keys()</span><br><span class="line">    ret = []  <span class="comment"># 编码结果</span></span><br><span class="line">    <span class="keyword">for</span> s <span class="keyword">in</span> sentence:  <span class="comment"># 遍历句子</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> s <span class="keyword">in</span> keys:  <span class="comment"># 字不在字典中，取未知字符</span></span><br><span class="line">            s = <span class="string">&quot;&lt;unk&gt;&quot;</span></span><br><span class="line">        ret.append(<span class="built_in">int</span>(dict_txt[s]))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> ret</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建执行器</span></span><br><span class="line">place = fluid.CPUPlace()</span><br><span class="line">exe = fluid.Executor(place)</span><br><span class="line">exe.run(fluid.default_startup_program())</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;加载模型&quot;</span>)</span><br><span class="line">infer_program, feeded_var_names, target_var = \</span><br><span class="line">    fluid.io.load_inference_model(dirname=model_save_dir, executor=exe)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成测试数据</span></span><br><span class="line">texts = []</span><br><span class="line">data1 = get_data(<span class="string">&quot;在获得诺贝尔文学奖7年之后，莫言15日晚间在山西汾阳贾家庄如是说&quot;</span>)</span><br><span class="line">data2 = get_data(<span class="string">&quot;综合&#x27;今日美国&#x27;、《世界日报》等当地媒体报道，芝加哥河滨警察局表示&quot;</span>)</span><br><span class="line">data3 = get_data(<span class="string">&quot;中国队无缘2020年世界杯&quot;</span>)</span><br><span class="line">data4 = get_data(<span class="string">&quot;中国人民银行今日发布通知，降低准备金率，预计释放4000亿流动性&quot;</span>)</span><br><span class="line">data5 = get_data(<span class="string">&quot;10月20日,第六届世界互联网大会正式开幕&quot;</span>)</span><br><span class="line">data6 = get_data(<span class="string">&quot;同一户型，为什么高层比低层要贵那么多？&quot;</span>)</span><br><span class="line">data7 = get_data(<span class="string">&quot;揭秘A股周涨5%资金动向：追捧2类股，抛售600亿香饽饽&quot;</span>)</span><br><span class="line">data8 = get_data(<span class="string">&quot;宋慧乔陷入感染危机，前夫宋仲基不戴口罩露面，身处国外神态轻松&quot;</span>)</span><br><span class="line">data9 = get_data(<span class="string">&quot;此盆栽花很好养，花美似牡丹，三季开花，南北都能养，很值得栽培&quot;</span>)<span class="comment">#不属于任何一个类别</span></span><br><span class="line"></span><br><span class="line">texts.append(data1)</span><br><span class="line">texts.append(data2)</span><br><span class="line">texts.append(data3)</span><br><span class="line">texts.append(data4)</span><br><span class="line">texts.append(data5)</span><br><span class="line">texts.append(data6)</span><br><span class="line">texts.append(data7)</span><br><span class="line">texts.append(data8)</span><br><span class="line">texts.append(data9)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取每个句子词数量</span></span><br><span class="line">base_shape = [[<span class="built_in">len</span>(c) <span class="keyword">for</span> c <span class="keyword">in</span> texts]]</span><br><span class="line"><span class="comment"># 生成数据</span></span><br><span class="line">tensor_words = fluid.create_lod_tensor(texts, base_shape, place)</span><br><span class="line"><span class="comment"># 执行预测</span></span><br><span class="line">result = exe.run(program=infer_program,</span><br><span class="line">                 feed=&#123;feeded_var_names[<span class="number">0</span>]: tensor_words&#125;, <span class="comment"># 待预测的数据</span></span><br><span class="line">                 fetch_list=target_var)</span><br><span class="line"></span><br><span class="line"><span class="comment"># print(result)</span></span><br><span class="line"></span><br><span class="line">names = [<span class="string">&quot;文化&quot;</span>, <span class="string">&quot;娱乐&quot;</span>, <span class="string">&quot;体育&quot;</span>, <span class="string">&quot;财经&quot;</span>, <span class="string">&quot;房产&quot;</span>, <span class="string">&quot;汽车&quot;</span>, <span class="string">&quot;教育&quot;</span>, <span class="string">&quot;科技&quot;</span>, <span class="string">&quot;国际&quot;</span>, <span class="string">&quot;证券&quot;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取最大值的索引</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(texts)):</span><br><span class="line">    lab = np.argsort(result)[<span class="number">0</span>][i][-<span class="number">1</span>]  <span class="comment"># 取出最大值的元素下标</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;预测结果：%d, 名称:%s, 概率:%f&quot;</span> % (lab, names[lab], result[<span class="number">0</span>][i][lab]))</span><br></pre></td></tr></table></figure>

<h2 id="中文情绪分析"><a href="#中文情绪分析" class="headerlink" title="中文情绪分析"></a>中文情绪分析</h2><ol>
<li>数据预处理与模型训练</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 中文情绪分析示例：数据预处理部分</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27; 数据集介绍</span></span><br><span class="line"><span class="string">中文酒店评论，7766笔数据，分为正面、负面评价</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="keyword">import</span> paddle</span><br><span class="line"><span class="keyword">import</span> paddle.dataset.imdb <span class="keyword">as</span> imdb</span><br><span class="line"><span class="keyword">import</span> paddle.fluid <span class="keyword">as</span> fluid</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> cpu_count</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数据预处理，将中文文字解析出来，并进行编码转换为数字，每一行文字存入数组</span></span><br><span class="line">mydict = &#123;&#125;  <span class="comment"># 存放出现的字及编码，格式： 好,1</span></span><br><span class="line">code = <span class="number">1</span></span><br><span class="line">data_file = <span class="string">&quot;data/hotel_discuss2.csv&quot;</span>  <span class="comment"># 原始样本路径</span></span><br><span class="line">dict_file = <span class="string">&quot;data/hotel_dict.txt&quot;</span> <span class="comment"># 字典文件路径</span></span><br><span class="line">encoding_file = <span class="string">&quot;data/hotel_encoding.txt&quot;</span> <span class="comment"># 编码后的样本文件路径</span></span><br><span class="line">puncts = <span class="string">&quot; \n&quot;</span>  <span class="comment"># 要剔除的标点符号列表</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(data_file, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&quot;utf-8-sig&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> f.readlines():</span><br><span class="line">        <span class="comment"># print(line)</span></span><br><span class="line">        trim_line = line.strip()</span><br><span class="line">        <span class="keyword">for</span> ch <span class="keyword">in</span> trim_line:</span><br><span class="line">            <span class="keyword">if</span> ch <span class="keyword">in</span> puncts:  <span class="comment"># 符号不参与编码</span></span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> ch <span class="keyword">in</span> mydict:  <span class="comment"># 已经在编码字典中</span></span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            <span class="keyword">elif</span> <span class="built_in">len</span>(ch) &lt;= <span class="number">0</span>:</span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            <span class="keyword">else</span>:  <span class="comment"># 当前文字没在字典中</span></span><br><span class="line">                mydict[ch] = code</span><br><span class="line">                code += <span class="number">1</span></span><br><span class="line">    code += <span class="number">1</span></span><br><span class="line">    mydict[<span class="string">&quot;&lt;unk&gt;&quot;</span>] = code  <span class="comment"># 未知字符</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 循环结束后，将字典存入字典文件</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(dict_file, <span class="string">&quot;w&quot;</span>, encoding=<span class="string">&quot;utf-8-sig&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    f.write(<span class="built_in">str</span>(mydict))</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;数据字典保存完成！&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将字典文件中的数据加载到mydict字典中</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">load_dict</span>():</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(dict_file, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&quot;utf-8-sig&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        lines = f.readlines()</span><br><span class="line">        new_dict = <span class="built_in">eval</span>(lines[<span class="number">0</span>])</span><br><span class="line">    <span class="keyword">return</span> new_dict</span><br><span class="line"></span><br><span class="line"><span class="comment"># 对评论数据进行编码</span></span><br><span class="line">new_dict = load_dict()  <span class="comment"># 调用函数加载</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(data_file, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&quot;utf-8-sig&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(encoding_file, <span class="string">&quot;w&quot;</span>, encoding=<span class="string">&quot;utf-8-sig&quot;</span>) <span class="keyword">as</span> fw:</span><br><span class="line">        <span class="keyword">for</span> line <span class="keyword">in</span> f.readlines():</span><br><span class="line">            label = line[<span class="number">0</span>]  <span class="comment"># 标签</span></span><br><span class="line">            remark = line[<span class="number">1</span>:-<span class="number">1</span>]  <span class="comment"># 评论</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> ch <span class="keyword">in</span> remark:</span><br><span class="line">                <span class="keyword">if</span> ch <span class="keyword">in</span> puncts:  <span class="comment"># 符号不参与编码</span></span><br><span class="line">                    <span class="keyword">continue</span></span><br><span class="line">                <span class="keyword">else</span>:</span><br><span class="line">                    fw.write(<span class="built_in">str</span>(mydict[ch]))</span><br><span class="line">                    fw.write(<span class="string">&quot;,&quot;</span>)</span><br><span class="line">            fw.write(<span class="string">&quot;\t&quot;</span> + <span class="built_in">str</span>(label) + <span class="string">&quot;\n&quot;</span>)  <span class="comment"># 写入tab分隔符、标签、换行符</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;数据预处理完成&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取字典的长度</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_dict_len</span>(<span class="params">dict_path</span>):</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(dict_path, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8-sig&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        lines = f.readlines()</span><br><span class="line">        new_dict = <span class="built_in">eval</span>(lines[<span class="number">0</span>])</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">len</span>(new_dict.keys())</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建数据读取器train_reader和test_reader</span></span><br><span class="line"><span class="comment"># 返回评论列表和标签</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">data_mapper</span>(<span class="params">sample</span>):</span><br><span class="line">    dt, lbl = sample</span><br><span class="line">    val = [<span class="built_in">int</span>(word) <span class="keyword">for</span> word <span class="keyword">in</span> dt.split(<span class="string">&quot;,&quot;</span>) <span class="keyword">if</span> word.isdigit()]</span><br><span class="line">    <span class="keyword">return</span> val, <span class="built_in">int</span>(lbl)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 随机从训练数据集文件中取出一行数据</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train_reader</span>(<span class="params">train_list_path</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">reader</span>():</span><br><span class="line">        <span class="keyword">with</span> <span class="built_in">open</span>(train_list_path, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&#x27;utf-8-sig&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">            lines = f.readlines()</span><br><span class="line">            np.random.shuffle(lines)  <span class="comment"># 打乱数据</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> line <span class="keyword">in</span> lines:</span><br><span class="line">                data, label = line.split(<span class="string">&quot;\t&quot;</span>)</span><br><span class="line">                <span class="keyword">yield</span> data, label</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 返回xmap_readers, 能够使用多线程方式读取数据</span></span><br><span class="line">    <span class="keyword">return</span> paddle.reader.xmap_readers(data_mapper,  <span class="comment"># 映射函数</span></span><br><span class="line">                                      reader,  <span class="comment"># 读取数据内容</span></span><br><span class="line">                                      cpu_count(),  <span class="comment"># 线程数量</span></span><br><span class="line">                                      <span class="number">1024</span>)  <span class="comment"># 读取数据队列大小</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义LSTM网络</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">lstm_net</span>(<span class="params">ipt, input_dim</span>):</span><br><span class="line">    ipt = fluid.layers.reshape(ipt, [-<span class="number">1</span>, <span class="number">1</span>],</span><br><span class="line">                               inplace=<span class="literal">True</span>) <span class="comment"># 是否替换，True则表示输入和返回是同一个对象</span></span><br><span class="line">    <span class="comment"># 词嵌入层</span></span><br><span class="line">    emb = fluid.layers.embedding(<span class="built_in">input</span>=ipt, size=[input_dim, <span class="number">128</span>], is_sparse=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 第一个全连接层</span></span><br><span class="line">    fc1 = fluid.layers.fc(<span class="built_in">input</span>=emb, size=<span class="number">128</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 第一分支：LSTM分支</span></span><br><span class="line">    lstm1, _ = fluid.layers.dynamic_lstm(<span class="built_in">input</span>=fc1, size=<span class="number">128</span>)</span><br><span class="line">    lstm2 = fluid.layers.sequence_pool(<span class="built_in">input</span>=lstm1, pool_type=<span class="string">&quot;max&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 第二分支</span></span><br><span class="line">    conv = fluid.layers.sequence_pool(<span class="built_in">input</span>=fc1, pool_type=<span class="string">&quot;max&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 输出层：全连接</span></span><br><span class="line">    out = fluid.layers.fc([conv, lstm2], size=<span class="number">2</span>, act=<span class="string">&quot;softmax&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> out</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义输入数据，lod_level不为0指定输入数据为序列数据</span></span><br><span class="line">dict_len = get_dict_len(dict_file)  <span class="comment"># 获取数据字典长度</span></span><br><span class="line">rmk = fluid.layers.data(name=<span class="string">&quot;rmk&quot;</span>, shape=[<span class="number">1</span>], dtype=<span class="string">&quot;int64&quot;</span>, lod_level=<span class="number">1</span>)</span><br><span class="line">label = fluid.layers.data(name=<span class="string">&quot;label&quot;</span>, shape=[<span class="number">1</span>], dtype=<span class="string">&quot;int64&quot;</span>)</span><br><span class="line"><span class="comment"># 定义长短期记忆网络</span></span><br><span class="line">model = lstm_net(rmk, dict_len)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义损失函数，情绪判断实际是一个分类任务，使用交叉熵作为损失函数</span></span><br><span class="line">cost = fluid.layers.cross_entropy(<span class="built_in">input</span>=model, label=label)</span><br><span class="line">avg_cost = fluid.layers.mean(cost)  <span class="comment"># 求损失值平均数</span></span><br><span class="line"><span class="comment"># layers.accuracy接口，用来评估预测准确率</span></span><br><span class="line">acc = fluid.layers.accuracy(<span class="built_in">input</span>=model, label=label)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义优化方法</span></span><br><span class="line"><span class="comment"># Adagrad(自适应学习率，前期放大梯度调节，后期缩小梯度调节)</span></span><br><span class="line">optimizer = fluid.optimizer.AdagradOptimizer(learning_rate=<span class="number">0.001</span>)</span><br><span class="line">opt = optimizer.minimize(avg_cost)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义网络</span></span><br><span class="line"><span class="comment"># place = fluid.CPUPlace()</span></span><br><span class="line">place = fluid.CUDAPlace(<span class="number">0</span>)</span><br><span class="line">exe = fluid.Executor(place)</span><br><span class="line">exe.run(fluid.default_startup_program())  <span class="comment"># 参数初始化</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义reader</span></span><br><span class="line">reader = train_reader(encoding_file)</span><br><span class="line">batch_train_reader = paddle.batch(reader, batch_size=<span class="number">128</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义输入数据的维度，数据的顺序是一条句子数据对应一个标签</span></span><br><span class="line">feeder = fluid.DataFeeder(place=place, feed_list=[rmk, label])</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> pass_id <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">40</span>):</span><br><span class="line">    <span class="keyword">for</span> batch_id, data <span class="keyword">in</span> <span class="built_in">enumerate</span>(batch_train_reader()):</span><br><span class="line">        train_cost, train_acc = exe.run(program=fluid.default_main_program(),</span><br><span class="line">                                        feed=feeder.feed(data),</span><br><span class="line">                                        fetch_list=[avg_cost, acc])</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> batch_id % <span class="number">20</span> == <span class="number">0</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;pass_id: %d, batch_id: %d, cost: %0.5f, acc:%.5f&quot;</span> %</span><br><span class="line">                  (pass_id, batch_id, train_cost[<span class="number">0</span>], train_acc))</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;模型训练完成......&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 保存模型</span></span><br><span class="line">model_save_dir = <span class="string">&quot;model/chn_emotion_analyses.model&quot;</span></span><br><span class="line"><span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(model_save_dir):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;create model path&quot;</span>)</span><br><span class="line">    os.makedirs(model_save_dir)</span><br><span class="line"></span><br><span class="line">fluid.io.save_inference_model(model_save_dir,  <span class="comment"># 保存路径</span></span><br><span class="line">                              feeded_var_names=[rmk.name],</span><br><span class="line">                              target_vars=[model],</span><br><span class="line">                              executor=exe)  <span class="comment"># Executor</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;模型保存完成, 保存路径: &quot;</span>, model_save_dir)</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>预测</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> paddle</span><br><span class="line"><span class="keyword">import</span> paddle.fluid <span class="keyword">as</span> fluid</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> cpu_count</span><br><span class="line"></span><br><span class="line">data_file = <span class="string">&quot;data/hotel_discuss2.csv&quot;</span></span><br><span class="line">dict_file = <span class="string">&quot;data/hotel_dict.txt&quot;</span></span><br><span class="line">encoding_file = <span class="string">&quot;data/hotel_encoding.txt&quot;</span></span><br><span class="line">model_save_dir = <span class="string">&quot;model/chn_emotion_analyses.model&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">load_dict</span>():</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(dict_file, <span class="string">&quot;r&quot;</span>, encoding=<span class="string">&quot;utf-8-sig&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        lines = f.readlines()</span><br><span class="line">        new_dict = <span class="built_in">eval</span>(lines[<span class="number">0</span>])</span><br><span class="line">        <span class="keyword">return</span> new_dict</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据字典对字符串进行编码</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">encode_by_dict</span>(<span class="params">remark, dict_encoded</span>):</span><br><span class="line">    remark = remark.strip()</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">len</span>(remark) &lt;= <span class="number">0</span>:</span><br><span class="line">        <span class="keyword">return</span> []</span><br><span class="line"></span><br><span class="line">    ret = []</span><br><span class="line">    <span class="keyword">for</span> ch <span class="keyword">in</span> remark:</span><br><span class="line">        <span class="keyword">if</span> ch <span class="keyword">in</span> dict_encoded:</span><br><span class="line">            ret.append(dict_encoded[ch])</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            ret.append(dict_encoded[<span class="string">&quot;&lt;unk&gt;&quot;</span>])</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> ret</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 编码,预测</span></span><br><span class="line">lods = []</span><br><span class="line">new_dict = load_dict()</span><br><span class="line">lods.append(encode_by_dict(<span class="string">&quot;总体来说房间非常干净,卫浴设施也相当不错,交通也比较便利&quot;</span>, new_dict))</span><br><span class="line">lods.append(encode_by_dict(<span class="string">&quot;酒店交通方便，环境也不错，正好是我们办事地点的旁边，感觉性价比还可以&quot;</span>, new_dict))</span><br><span class="line">lods.append(encode_by_dict(<span class="string">&quot;设施还可以，服务人员态度也好，交通还算便利&quot;</span>, new_dict))</span><br><span class="line">lods.append(encode_by_dict(<span class="string">&quot;酒店服务态度极差，设施很差&quot;</span>, new_dict))</span><br><span class="line">lods.append(encode_by_dict(<span class="string">&quot;我住过的最不好的酒店,以后决不住了&quot;</span>, new_dict))</span><br><span class="line">lods.append(encode_by_dict(<span class="string">&quot;说实在的我很失望，我想这家酒店以后无论如何我都不会再去了&quot;</span>, new_dict))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取每句话的单词数量</span></span><br><span class="line">base_shape = [[<span class="built_in">len</span>(c) <span class="keyword">for</span> c <span class="keyword">in</span> lods]]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成预测数据</span></span><br><span class="line">place = fluid.CPUPlace()</span><br><span class="line">infer_exe = fluid.Executor(place)</span><br><span class="line">infer_exe.run(fluid.default_startup_program())</span><br><span class="line"></span><br><span class="line">tensor_words = fluid.create_lod_tensor(lods, base_shape, place)</span><br><span class="line"></span><br><span class="line">infer_program, feed_target_names, fetch_targets = fluid.io.load_inference_model(dirname=model_save_dir, executor=infer_exe)</span><br><span class="line"><span class="comment"># tvar = np.array(fetch_targets, dtype=&quot;int64&quot;)</span></span><br><span class="line">results = infer_exe.run(program=infer_program,</span><br><span class="line">                  feed=&#123;feed_target_names[<span class="number">0</span>]: tensor_words&#125;,</span><br><span class="line">                  fetch_list=fetch_targets)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 打印每句话的正负面预测概率</span></span><br><span class="line"><span class="keyword">for</span> i, r <span class="keyword">in</span> <span class="built_in">enumerate</span>(results[<span class="number">0</span>]):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;负面: %0.5f, 正面: %0.5f&quot;</span> % (r[<span class="number">0</span>], r[<span class="number">1</span>]))</span><br></pre></td></tr></table></figure>

</article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%AD%A6%E4%B9%A0/">学习</a><a class="post-meta__tags" href="/tags/%E8%AE%B0%E5%BD%95/">记录</a><a class="post-meta__tags" href="/tags/Python/">Python</a><a class="post-meta__tags" href="/tags/%E7%AC%94%E8%AE%B0/">笔记</a><a class="post-meta__tags" href="/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a></div><div class="post_share"><div class="social-share" data-image="https://image.discover304.top/ai/dl/space_work.jpg?imageView2/2/h/300" data-sites="facebook,twitter,wechat,weibo,qzone,qq,linkedin"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> Donate<div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" data-lazy-src="/img/wechat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" data-lazy-src="/img/alipay.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/12/01/2021q4/109-dimension-up/"><img class="prev-cover" data-lazy-src="https://image.discover304.top/huashi6-shichen.jpeg?imageView2/2/h/300" onerror="onerror=null;src='/img/404.png'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">暴论：人工智能可以进行可解释升维</div></div></a></div><div class="next-post pull-right"><a href="/2021/12/01/2021q4/108-2-dl-ex/"><img class="next-cover" data-lazy-src="https://image.discover304.top/ai/dl/space_work.jpg?imageView2/2/h/300" onerror="onerror=null;src='/img/404.png'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">【深度学习】实例第三部分：TensorFlow</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2021/11/30/2021q4/107-0-dl/" title="【深度学习】概述"><img class="cover" data-lazy-src="https://image.discover304.top/ai/dl/machine-girl.jpg?imageView2/2/h/300" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-12-25</div><div class="title">【深度学习】概述</div></div></a></div><div><a href="/2021/11/30/2021q4/107-1-dl-back/" title="【深度学习】基础 叁：反向传播算法"><img class="cover" data-lazy-src="https://image.discover304.top/ai/dl/machine-girl.jpg?imageView2/2/h/300" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-12-25</div><div class="title">【深度学习】基础 叁：反向传播算法</div></div></a></div><div><a href="/2021/11/30/2021q4/107-1-dl-loss-gd/" title="【深度学习】基础 贰：损失函数与梯度下降"><img class="cover" data-lazy-src="https://image.discover304.top/ai/dl/machine-girl.jpg?imageView2/2/h/300" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-12-25</div><div class="title">【深度学习】基础 贰：损失函数与梯度下降</div></div></a></div><div><a href="/2021/11/30/2021q4/107-1-dl-cnn/" title="【深度学习】基础 肆：卷积神经网络"><img class="cover" data-lazy-src="https://image.discover304.top/ai/dl/machine-girl.jpg?imageView2/2/h/300" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-12-25</div><div class="title">【深度学习】基础 肆：卷积神经网络</div></div></a></div><div><a href="/2021/11/30/2021q4/107-1-dl-perceptron/" title="【深度学习】基础 壹：感知机与神经网络"><img class="cover" data-lazy-src="https://image.discover304.top/ai/dl/machine-girl.jpg?imageView2/2/h/300" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-12-25</div><div class="title">【深度学习】基础 壹：感知机与神经网络</div></div></a></div><div><a href="/2021/12/02/2021q4/107-4-dl-pdpd-base/" title="【深度学习】框架：PaddlePaddle基础"><img class="cover" data-lazy-src="https://image.discover304.top/ai/dl/02/dog-end-world.jpg?imageView2/2/h/300" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-12-25</div><div class="title">【深度学习】框架：PaddlePaddle基础</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> Comment</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside_content" id="aside_content"><div class="card-widget card-info"><div class="card-content"><div class="card-info-avatar is-center"><img class="avatar-img" data-lazy-src="/img/head.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">✨YangSier✨</div><div class="author-info__description">Love Everything You Like.</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">243</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">88</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">Categories</div><div class="length-num">23</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://space.bilibili.com/98639326"><i class="fab fa-bilibili"></i><span>Bilibili Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Discover304" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/Discover304" target="_blank" title="CSDN"><i class="fa-solid fa-c"></i></a><a class="social-icon" href="https://www.zhihu.com/people/discover-56-86-75" target="_blank" title="知乎"><i class="fa-brands fa-zhihu"></i></a><a class="social-icon" href="mailto:hobart.yang@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://jq.qq.com/?_wv=1027&amp;k=EaGddTQg" target="_blank" title="QQ"><i class="fa-brands fa-qq"></i></a></div></div></div><div class="card-widget card-announcement"><div class="card-content"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>Announcement</span></div><div class="announcement_content">✨动态更新：<p style="text-align:center">享受精彩大学生活中。</p>✨聊天划水QQ群：<p style="text-align:center"><a target="_blank" rel="noopener" href="https://jq.qq.com/?_wv=1027&k=EaGddTQg"><strong>兔叽の魔术工房</strong></a><br>942-848-525</p>✨我们的口号是：<p style="text-align:center; color:#39C5BB">人工降神，机械飞升！</p><a target="_blank" rel="noopener" href='https://space.bilibili.com/98639326'><img src='/img/mikulittletrans.png'></a></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="card-content"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Helloworld"><span class="toc-text">Helloworld</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%A0%E9%87%8F%E6%93%8D%E4%BD%9C"><span class="toc-text">张量操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%80%E5%8D%95%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92"><span class="toc-text">简单线性回归</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%A2%E5%A3%AB%E9%A1%BF%E6%88%BF%E4%BB%B7%E9%A2%84%E6%B5%8B"><span class="toc-text">波士顿房价预测</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A2%9E%E9%87%8F%E6%A8%A1%E5%9E%8B%E8%AE%AD%E7%BB%83"><span class="toc-text">增量模型训练</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B0%B4%E6%9E%9C%E8%AF%86%E5%88%AB"><span class="toc-text">水果识别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A9%E7%94%A8VGG%E5%AE%9E%E7%8E%B0%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB"><span class="toc-text">利用VGG实现图像分类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%AD%E6%96%87%E6%96%87%E6%9C%AC%E5%88%86%E7%B1%BB"><span class="toc-text">中文文本分类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%AD%E6%96%87%E6%83%85%E7%BB%AA%E5%88%86%E6%9E%90"><span class="toc-text">中文情绪分析</span></a></li></ol></div></div></div><div class="card-widget card-recent-post"><div class="card-content"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/04/24/2023q1/173-nginx-docker-blog-page-depoly/" title="【模板】Hexo Docker Nginx 个人博客服务器部署"><img data-lazy-src="https://image.discover304.top/s16001804242023.png?imageView2/2/h/300" onerror="this.onerror=null;this.src='/img/404.png'" alt="【模板】Hexo Docker Nginx 个人博客服务器部署"/></a><div class="content"><a class="title" href="/2023/04/24/2023q1/173-nginx-docker-blog-page-depoly/" title="【模板】Hexo Docker Nginx 个人博客服务器部署">【模板】Hexo Docker Nginx 个人博客服务器部署</a><time datetime="2023-04-24T06:39:24.000Z" title="Created 2023-04-24 14:39:24">2023-04-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/12/11/2022q4/172-design-idea/" title="【思考】创新点子"><img data-lazy-src="https://image.discover304.top/blog-img/s19313212112022.png" onerror="this.onerror=null;this.src='/img/404.png'" alt="【思考】创新点子"/></a><div class="content"><a class="title" href="/2022/12/11/2022q4/172-design-idea/" title="【思考】创新点子">【思考】创新点子</a><time datetime="2022-12-11T11:08:37.000Z" title="Created 2022-12-11 19:08:37">2022-12-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/12/11/2022q4/171-other-thought/" title="【思考】其他思考"><img data-lazy-src="https://image.discover304.top/blog-img/s19305112112022.png" onerror="this.onerror=null;this.src='/img/404.png'" alt="【思考】其他思考"/></a><div class="content"><a class="title" href="/2022/12/11/2022q4/171-other-thought/" title="【思考】其他思考">【思考】其他思考</a><time datetime="2022-12-11T11:08:17.000Z" title="Created 2022-12-11 19:08:17">2022-12-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/12/11/2022q4/170-key-thought/" title="【思考】核心思考"><img data-lazy-src="https://image.discover304.top/blog-img/s19294112112022.png" onerror="this.onerror=null;this.src='/img/404.png'" alt="【思考】核心思考"/></a><div class="content"><a class="title" href="/2022/12/11/2022q4/170-key-thought/" title="【思考】核心思考">【思考】核心思考</a><time datetime="2022-12-11T11:08:06.000Z" title="Created 2022-12-11 19:08:06">2022-12-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/12/09/2022q4/169-new-r/" title="R: Getting Into Project of R"><img data-lazy-src="https://image.discover304.top/blog-img/s18350812112022.png" onerror="this.onerror=null;this.src='/img/404.png'" alt="R: Getting Into Project of R"/></a><div class="content"><a class="title" href="/2022/12/09/2022q4/169-new-r/" title="R: Getting Into Project of R">R: Getting Into Project of R</a><time datetime="2022-12-09T09:44:06.000Z" title="Created 2022-12-09 17:44:06">2022-12-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/11/28/2022q3/167-2-sp-review/" title="SP Modules Review Contents (3)"><img data-lazy-src="https://image.discover304.top/blog-img/s11220010012022.png?imageView2/2/h/300" onerror="this.onerror=null;this.src='/img/404.png'" alt="SP Modules Review Contents (3)"/></a><div class="content"><a class="title" href="/2022/11/28/2022q3/167-2-sp-review/" title="SP Modules Review Contents (3)">SP Modules Review Contents (3)</a><time datetime="2022-11-28T07:24:39.000Z" title="Created 2022-11-28 15:24:39">2022-11-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/11/17/2022q3/166-10-connected-speech-hidden-markov-model/" title="SP Module 10 Connected Speech &amp; HMM Training"><img data-lazy-src="https://image.discover304.top/blog-img/s11220010012022.png?imageView2/2/h/300" onerror="this.onerror=null;this.src='/img/404.png'" alt="SP Module 10 Connected Speech &amp; HMM Training"/></a><div class="content"><a class="title" href="/2022/11/17/2022q3/166-10-connected-speech-hidden-markov-model/" title="SP Module 10 Connected Speech &amp; HMM Training">SP Module 10 Connected Speech &amp; HMM Training</a><time datetime="2022-11-17T01:47:12.000Z" title="Created 2022-11-17 09:47:12">2022-11-17</time></div></div></div></div></div></div></div></main><footer id="footer" style="background-image: url(https://image.discover304.top/ai/dl/space_work.jpg?imageView2/2/h/300)"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By ✨YangSier✨</div><div><a target="_blank" href="https://beian.miit.gov.cn/" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;"> 冀ICP备2021025381号-1</p></a></div><div><a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=13060602001430" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="/img/beian.png" style="float:left;"/><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;">冀公网安备 13060602001430号</p></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="Switch Between Traditional Chinese And Simplified Chinese">繁</button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="Scroll To Comments"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">Local search</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="Search for Posts" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.spacingElementById('content-inner')
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.spacingElementById('content-inner')
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> {preloader.endLoading()})</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'neutral',
      })
      false && mermaid.init()
    })
  }
}</script><script>function loadValine () {
  function initValine () {
    let initData = {
      el: '#vcomment',
      appId: 'A9RWVELPcIotgfbpp9KLGXQM-gzGzoHsz',
      appKey: 'MLgPQW5h0DPgE8jNkeREKubU',
      placeholder: '欢迎留言呀。（网址是选填，可以留空）',
      avatar: 'monsterid',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'zh-CN',
      recordIP: true,
      serverURLs: 'https://a9rwvelp.lc-cn-n1-shared.com',
      emojiCDN: 'https://cdn.jsdelivr.net/gh/GamerNoTitle/ValineCDN@master/',
      emojiMaps: {"QQ1":"QQ/aini.gif","QQ2":"QQ/aixin.gif","QQ3":"QQ/aoman.gif","QQ4":"QQ/baiyan.gif","QQ5":"QQ/bangbangtang.gif","QQ6":"QQ/baojin.gif","QQ7":"QQ/baoquan.gif","QQ8":"QQ/bishi.gif","QQ9":"QQ/bizui.gif","QQ11":"QQ/cahan.gif","QQ12":"QQ/caidao.gif","QQ13":"QQ/chi.gif","QQ14":"QQ/ciya.gif","QQ15":"QQ/dabing.gif","QQ16":"QQ/daku.gif","QQ17":"QQ/dan.gif","QQ18":"QQ/deyi.gif","QQ19":"QQ/doge.gif","QQ20":"QQ/fadai.gif","QQ21":"QQ/fanu.gif","QQ22":"QQ/fendou.gif","QQ23":"QQ/ganga.gif","QQ24":"QQ/gouyin.gif","QQ25":"QQ/guzhang.gif","QQ26":"QQ/haixiu.gif","QQ27":"QQ/hanxiao.gif","QQ28":"QQ/haobang.gif","QQ29":"QQ/haqian.gif","QQ30":"QQ/hecai.gif","QQ31":"QQ/hexie.gif","QQ32":"QQ/huaixiao.gif","QQ33":"QQ/jie.gif","QQ34":"QQ/jingkong.gif","QQ35":"QQ/jingxi.gif","QQ36":"QQ/jingya.gif","QQ37":"QQ/juhua.gif","QQ38":"QQ/keai.gif","QQ39":"QQ/kelian.gif","QQ40":"QQ/koubi.gif","QQ41":"QQ/ku.gif","QQ42":"QQ/kuaikule.gif","QQ43":"QQ/kulou.gif","QQ44":"QQ/kun.gif","QQ45":"QQ/lanqiu.gif","QQ46":"QQ/leiben.gif","QQ47":"QQ/lenghan.gif","QQ48":"QQ/liuhan.gif","QQ49":"QQ/liulei.gif","QQ50":"QQ/nanguo.gif","QQ51":"QQ/OK.gif","QQ52":"QQ/penxue.gif","QQ53":"QQ/piezui.gif","QQ54":"QQ/pijiu.gif","QQ55":"QQ/qiang.gif","QQ56":"QQ/qiaoda.gif","QQ57":"QQ/qinqin.gif","QQ58":"QQ/qiudale.gif","QQ59":"QQ/quantou.gif","QQ60":"QQ/saorao.gif","QQ61":"QQ/se.gif","QQ62":"QQ/shengli.gif","QQ63":"QQ/shouqiang.gif","QQ64":"QQ/shuai.gif","QQ65":"QQ/shui.gif","QQ66":"QQ/tiaopi.gif","QQ67":"QQ/touxiao.gif","QQ68":"QQ/tu.gif","QQ69":"QQ/tuosai.gif","QQ70":"QQ/weiqu.gif","QQ71":"QQ/weixiao.gif","QQ72":"QQ/woshou.gif","QQ73":"QQ/wozuimei.gif","QQ74":"QQ/wunai.gif","QQ75":"QQ/xia.gif","QQ76":"QQ/xiaojiujie.gif","QQ77":"QQ/xiaoku.gif","QQ78":"QQ/xiaoyanger.gif","QQ79":"QQ/xieyanxiao.gif","QQ80":"QQ/xigua.gif","QQ81":"QQ/xu.gif","QQ82":"QQ/yangtuo.gif","QQ83":"QQ/yinxian.gif","QQ84":"QQ/yiwen.gif","QQ85":"QQ/youhengheng.gif","QQ86":"QQ/youling.gif","QQ87":"QQ/yun.gif","QQ88":"QQ/zaijian.gif","QQ89":"QQ/zhayanjian.gif","QQ90":"QQ/zhemo.gif","QQ91":"QQ/zhouma.gif","QQ92":"QQ/zhuakuang.gif","QQ93":"QQ/zuohengheng.gif","bilibiliHotKey1":"bilibiliHotKey/1.jpg","bilibiliHotKey2":"bilibiliHotKey/10.jpg","bilibiliHotKey3":"bilibiliHotKey/11.jpg","bilibiliHotKey4":"bilibiliHotKey/12.jpg","bilibiliHotKey5":"bilibiliHotKey/13.jpg","bilibiliHotKey6":"bilibiliHotKey/14.jpg","bilibiliHotKey7":"bilibiliHotKey/15.jpg","bilibiliHotKey8":"bilibiliHotKey/16.jpg","bilibiliHotKey9":"bilibiliHotKey/17.jpg","bilibiliHotKey10":"bilibiliHotKey/18.jpg","bilibiliHotKey11":"bilibiliHotKey/19.jpg","bilibiliHotKey12":"bilibiliHotKey/2.jpg","bilibiliHotKey13":"bilibiliHotKey/20.jpg","bilibiliHotKey14":"bilibiliHotKey/21.jpg","bilibiliHotKey15":"bilibiliHotKey/22.jpg","bilibiliHotKey16":"bilibiliHotKey/23.jpg","bilibiliHotKey17":"bilibiliHotKey/24.jpg","bilibiliHotKey18":"bilibiliHotKey/25.jpg","bilibiliHotKey19":"bilibiliHotKey/26.jpg","bilibiliHotKey20":"bilibiliHotKey/27.jpg","bilibiliHotKey21":"bilibiliHotKey/28.jpg","bilibiliHotKey22":"bilibiliHotKey/29.jpg","bilibiliHotKey23":"bilibiliHotKey/3.jpg","bilibiliHotKey24":"bilibiliHotKey/30.jpg","bilibiliHotKey25":"bilibiliHotKey/31.jpg","bilibiliHotKey26":"bilibiliHotKey/32.jpg","bilibiliHotKey27":"bilibiliHotKey/4.jpg","bilibiliHotKey28":"bilibiliHotKey/5.jpg","bilibiliHotKey29":"bilibiliHotKey/6.jpg","bilibiliHotKey30":"bilibiliHotKey/7.jpg","bilibiliHotKey31":"bilibiliHotKey/8.jpg","bilibiliHotKey32":"bilibiliHotKey/9.jpg","Menhera-chan1":"Menhera-chan/1.jpg","Menhera-chan2":"Menhera-chan/10.jpg","Menhera-chan3":"Menhera-chan/100.jpg","Menhera-chan4":"Menhera-chan/101.jpg","Menhera-chan5":"Menhera-chan/102.jpg","Menhera-chan6":"Menhera-chan/103.jpg","Menhera-chan7":"Menhera-chan/104.jpg","Menhera-chan8":"Menhera-chan/105.jpg","Menhera-chan9":"Menhera-chan/106.jpg","Menhera-chan10":"Menhera-chan/107.jpg","Menhera-chan11":"Menhera-chan/108.jpg","Menhera-chan12":"Menhera-chan/109.jpg","Menhera-chan13":"Menhera-chan/11.jpg","Menhera-chan14":"Menhera-chan/110.jpg","Menhera-chan15":"Menhera-chan/111.jpg","Menhera-chan16":"Menhera-chan/112.jpg","Menhera-chan17":"Menhera-chan/113.jpg","Menhera-chan18":"Menhera-chan/114.jpg","Menhera-chan19":"Menhera-chan/115.jpg","Menhera-chan20":"Menhera-chan/116.jpg","Menhera-chan21":"Menhera-chan/117.jpg","Menhera-chan22":"Menhera-chan/118.jpg","Menhera-chan23":"Menhera-chan/119.jpg","Menhera-chan24":"Menhera-chan/12.jpg","Menhera-chan25":"Menhera-chan/120.jpg","Menhera-chan26":"Menhera-chan/13.jpg","Menhera-chan27":"Menhera-chan/14.jpg","Menhera-chan28":"Menhera-chan/15.jpg","Menhera-chan29":"Menhera-chan/16.jpg","Menhera-chan30":"Menhera-chan/17.jpg","Menhera-chan31":"Menhera-chan/18.jpg","Menhera-chan32":"Menhera-chan/19.jpg","Menhera-chan33":"Menhera-chan/2.jpg","Menhera-chan34":"Menhera-chan/20.jpg","Menhera-chan35":"Menhera-chan/21.jpg","Menhera-chan36":"Menhera-chan/22.jpg","Menhera-chan37":"Menhera-chan/23.jpg","Menhera-chan38":"Menhera-chan/24.jpg","Menhera-chan39":"Menhera-chan/25.jpg","Menhera-chan40":"Menhera-chan/26.jpg","Menhera-chan41":"Menhera-chan/27.jpg","Menhera-chan42":"Menhera-chan/28.jpg","Menhera-chan43":"Menhera-chan/29.jpg","Menhera-chan44":"Menhera-chan/3.jpg","Menhera-chan45":"Menhera-chan/30.jpg","Menhera-chan46":"Menhera-chan/31.jpg","Menhera-chan47":"Menhera-chan/32.jpg","Menhera-chan48":"Menhera-chan/33.jpg","Menhera-chan49":"Menhera-chan/34.jpg","Menhera-chan50":"Menhera-chan/35.jpg","Menhera-chan51":"Menhera-chan/36.jpg","Menhera-chan52":"Menhera-chan/37.jpg","Menhera-chan53":"Menhera-chan/38.jpg","Menhera-chan54":"Menhera-chan/39.jpg","Menhera-chan55":"Menhera-chan/4.jpg","Menhera-chan56":"Menhera-chan/40.jpg","Menhera-chan57":"Menhera-chan/41.jpg","Menhera-chan58":"Menhera-chan/42.jpg","Menhera-chan59":"Menhera-chan/43.jpg","Menhera-chan60":"Menhera-chan/44.jpg","Menhera-chan61":"Menhera-chan/45.jpg","Menhera-chan62":"Menhera-chan/46.jpg","Menhera-chan63":"Menhera-chan/47.jpg","Menhera-chan64":"Menhera-chan/48.jpg","Menhera-chan65":"Menhera-chan/49.jpg","Menhera-chan66":"Menhera-chan/5.jpg","Menhera-chan67":"Menhera-chan/50.jpg","Menhera-chan68":"Menhera-chan/51.jpg","Menhera-chan69":"Menhera-chan/52.jpg","Menhera-chan70":"Menhera-chan/53(1).jpg","Menhera-chan71":"Menhera-chan/53.jpg","Menhera-chan72":"Menhera-chan/54.jpg","Menhera-chan73":"Menhera-chan/55.jpg","Menhera-chan74":"Menhera-chan/56.jpg","Menhera-chan75":"Menhera-chan/57.jpg","Menhera-chan76":"Menhera-chan/58.jpg","Menhera-chan77":"Menhera-chan/59.jpg","Menhera-chan78":"Menhera-chan/6.jpg","Menhera-chan79":"Menhera-chan/60.jpg","Menhera-chan80":"Menhera-chan/61.jpg","Menhera-chan81":"Menhera-chan/62.jpg","Menhera-chan82":"Menhera-chan/63.jpg","Menhera-chan83":"Menhera-chan/64.jpg","Menhera-chan84":"Menhera-chan/65.jpg","Menhera-chan85":"Menhera-chan/66.jpg","Menhera-chan86":"Menhera-chan/67.jpg","Menhera-chan87":"Menhera-chan/68.jpg","Menhera-chan88":"Menhera-chan/69.jpg","Menhera-chan89":"Menhera-chan/7.jpg","Menhera-chan90":"Menhera-chan/70.jpg","Menhera-chan91":"Menhera-chan/71.jpg","Menhera-chan92":"Menhera-chan/72.jpg","Menhera-chan93":"Menhera-chan/73.jpg","Menhera-chan94":"Menhera-chan/74.jpg","Menhera-chan95":"Menhera-chan/75.jpg","Menhera-chan96":"Menhera-chan/76.jpg","Menhera-chan97":"Menhera-chan/77.jpg","Menhera-chan98":"Menhera-chan/78.jpg","Menhera-chan99":"Menhera-chan/79.jpg","Menhera-chan100":"Menhera-chan/8.jpg","Menhera-chan101":"Menhera-chan/80.jpg","Menhera-chan102":"Menhera-chan/81.jpg","Menhera-chan103":"Menhera-chan/82.jpg","Menhera-chan104":"Menhera-chan/83.jpg","Menhera-chan105":"Menhera-chan/84.jpg","Menhera-chan106":"Menhera-chan/85.jpg","Menhera-chan107":"Menhera-chan/86.jpg","Menhera-chan108":"Menhera-chan/87.jpg","Menhera-chan109":"Menhera-chan/88.jpg","Menhera-chan110":"Menhera-chan/89.jpg","Menhera-chan111":"Menhera-chan/9.jpg","Menhera-chan112":"Menhera-chan/90.jpg","Menhera-chan113":"Menhera-chan/91.jpg","Menhera-chan114":"Menhera-chan/92.jpg","Menhera-chan115":"Menhera-chan/93.jpg","Menhera-chan116":"Menhera-chan/94.jpg","Menhera-chan117":"Menhera-chan/95.jpg","Menhera-chan118":"Menhera-chan/96.jpg","Menhera-chan119":"Menhera-chan/97.jpg","Menhera-chan120":"Menhera-chan/98.jpg","Menhera-chan121":"Menhera-chan/99.jpg","Sweetie-Bunny1":"Sweetie-Bunny/12311678.png","Sweetie-Bunny2":"Sweetie-Bunny/12311679.png","Sweetie-Bunny3":"Sweetie-Bunny/12311680.png","Sweetie-Bunny4":"Sweetie-Bunny/12311681.png","Sweetie-Bunny5":"Sweetie-Bunny/12311682.png","Sweetie-Bunny6":"Sweetie-Bunny/12311683.png","Sweetie-Bunny7":"Sweetie-Bunny/12311684.png","Sweetie-Bunny8":"Sweetie-Bunny/12311685.png","Sweetie-Bunny9":"Sweetie-Bunny/12311686.png","Sweetie-Bunny10":"Sweetie-Bunny/12311687.png","Sweetie-Bunny11":"Sweetie-Bunny/12311688.png","Sweetie-Bunny12":"Sweetie-Bunny/12311689.png","Sweetie-Bunny13":"Sweetie-Bunny/12311690.png","Sweetie-Bunny14":"Sweetie-Bunny/12311691.png","Sweetie-Bunny15":"Sweetie-Bunny/12311692.png","Sweetie-Bunny16":"Sweetie-Bunny/12311693.png","Sweetie-Bunny17":"Sweetie-Bunny/12311694.png","Sweetie-Bunny18":"Sweetie-Bunny/12311695.png","Sweetie-Bunny19":"Sweetie-Bunny/12311696.png","Sweetie-Bunny20":"Sweetie-Bunny/12311697.png","Sweetie-Bunny21":"Sweetie-Bunny/12311698.png","Sweetie-Bunny22":"Sweetie-Bunny/12311699.png","Sweetie-Bunny23":"Sweetie-Bunny/12311700.png","Sweetie-Bunny24":"Sweetie-Bunny/12311701.png","Sweetie-Bunny25":"Sweetie-Bunny/12311702.png","Sweetie-Bunny26":"Sweetie-Bunny/12311703.png","Sweetie-Bunny27":"Sweetie-Bunny/12311704.png","Sweetie-Bunny28":"Sweetie-Bunny/12311705.png","Sweetie-Bunny29":"Sweetie-Bunny/12311706.png","Sweetie-Bunny30":"Sweetie-Bunny/12311707.png","Sweetie-Bunny31":"Sweetie-Bunny/12311708.png","Sweetie-Bunny32":"Sweetie-Bunny/12311709.png","Sweetie-Bunny33":"Sweetie-Bunny/12311710.png","Sweetie-Bunny34":"Sweetie-Bunny/12311711.png","Sweetie-Bunny35":"Sweetie-Bunny/12311712.png","Sweetie-Bunny36":"Sweetie-Bunny/12311713.png","Sweetie-Bunny37":"Sweetie-Bunny/12311714.png","Sweetie-Bunny38":"Sweetie-Bunny/12311715.png","Sweetie-Bunny39":"Sweetie-Bunny/12311716.png","Sweetie-Bunny40":"Sweetie-Bunny/12311717.png","Majotabi1":"Majotabi/367516718.png","Majotabi2":"Majotabi/367516719.png","Majotabi3":"Majotabi/367516720.png","Majotabi4":"Majotabi/367516721.png","Majotabi5":"Majotabi/367516722.png","Majotabi6":"Majotabi/367516723.png","Majotabi7":"Majotabi/367516724.png","Majotabi8":"Majotabi/367516725.png","Majotabi9":"Majotabi/367516726.png","Majotabi10":"Majotabi/367516727.png","Majotabi11":"Majotabi/367516728.png","Majotabi12":"Majotabi/367516729.png","Majotabi13":"Majotabi/367516730.png","Majotabi14":"Majotabi/367516731.png","Majotabi15":"Majotabi/367516732.png","Majotabi16":"Majotabi/367516733.png","Majotabi17":"Majotabi/367516734.png","Majotabi18":"Majotabi/367516735.png","Majotabi19":"Majotabi/367516736.png","Majotabi20":"Majotabi/367516737.png","Majotabi21":"Majotabi/367516738.png","Majotabi22":"Majotabi/367516739.png","Majotabi23":"Majotabi/367516740.png","Majotabi24":"Majotabi/367516741.png","Majotabi25":"Majotabi/367516742.png","Majotabi26":"Majotabi/367516743.png","Majotabi27":"Majotabi/367516744.png","Majotabi28":"Majotabi/367516745.png","Majotabi29":"Majotabi/367516746.png","Majotabi30":"Majotabi/367516747.png","Majotabi31":"Majotabi/367516748.png","Majotabi32":"Majotabi/367516749.png","Majotabi33":"Majotabi/367516750.png","Majotabi34":"Majotabi/367516751.png","Majotabi35":"Majotabi/367516752.png","Majotabi36":"Majotabi/367516753.png","Majotabi37":"Majotabi/367516754.png","Majotabi38":"Majotabi/367516755.png","Majotabi39":"Majotabi/367516756.png","Majotabi40":"Majotabi/367516757.png","Snow-Miku1":"Snow-Miku/3583066@2x.png","Snow-Miku2":"Snow-Miku/3583067@2x.png","Snow-Miku3":"Snow-Miku/3583068@2x.png","Snow-Miku4":"Snow-Miku/3583069@2x.png","Snow-Miku5":"Snow-Miku/3583070@2x.png","Snow-Miku6":"Snow-Miku/3583071@2x.png","Snow-Miku7":"Snow-Miku/3583072@2x.png","Snow-Miku8":"Snow-Miku/3583073@2x.png","Snow-Miku9":"Snow-Miku/3583074@2x.png","Snow-Miku10":"Snow-Miku/3583075@2x.png","Snow-Miku11":"Snow-Miku/3583076@2x.png","Snow-Miku12":"Snow-Miku/3583077@2x.png","Snow-Miku13":"Snow-Miku/3583078@2x.png","Snow-Miku14":"Snow-Miku/3583079@2x.png","Snow-Miku15":"Snow-Miku/3583080@2x.png","Snow-Miku16":"Snow-Miku/3583081@2x.png","Snow-Miku17":"Snow-Miku/3583082@2x.png","Snow-Miku18":"Snow-Miku/3583083@2x.png","Snow-Miku19":"Snow-Miku/3583084@2x.png","Snow-Miku20":"Snow-Miku/3583085@2x.png","Snow-Miku21":"Snow-Miku/3583086@2x.png","Snow-Miku22":"Snow-Miku/3583087@2x.png","Snow-Miku23":"Snow-Miku/3583088@2x.png","Snow-Miku24":"Snow-Miku/3583089@2x.png","Snow-Miku25":"Snow-Miku/3583090@2x.png","Snow-Miku26":"Snow-Miku/3583091@2x.png","Snow-Miku27":"Snow-Miku/3583092@2x.png","Snow-Miku28":"Snow-Miku/3583093@2x.png","Snow-Miku29":"Snow-Miku/3583094@2x.png","Snow-Miku30":"Snow-Miku/3583095@2x.png","Snow-Miku31":"Snow-Miku/3583096@2x.png","Snow-Miku32":"Snow-Miku/3583097@2x.png","Snow-Miku33":"Snow-Miku/3583098@2x.png","Snow-Miku34":"Snow-Miku/3583099@2x.png","Snow-Miku35":"Snow-Miku/3583100@2x.png","Snow-Miku36":"Snow-Miku/3583101@2x.png","Snow-Miku37":"Snow-Miku/3583102@2x.png","Snow-Miku38":"Snow-Miku/3583103@2x.png","Snow-Miku39":"Snow-Miku/3583104@2x.png","Snow-Miku40":"Snow-Miku/3583105@2x.png"},
      enableQQ: true,
      path: window.location.pathname,
    }

    if (true) { 
      initData.requiredFields= ('nick,mail'.split(','))
    }
    
    if (false) {
      const otherData = false
      initData = Object.assign({}, initData, otherData)
    }
    
    const valine = new Valine(initData)
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.querySelector('#vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script>(function(){
  const bp = document.createElement('script');
  const curProtocol = window.location.protocol.split(':')[0];
  if (curProtocol === 'https') {
    bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else{
    bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
  bp.dataset.pjax = ''
  const s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(bp, s);
})()</script></div></body></html>